HP-65 prime factor finder
10-26-2014, 07:16 PM (This post was last modified: 01-27-2017 12:55 AM by Don Shepherd.)
Post: #1
 Don Shepherd Senior Member Posts: 569 Joined: Dec 2013
HP-65 prime factor finder
This program finds the prime factors of an integer on the HP-65 calculator. It is based on Dave Britten's modification to an original HP-67 program, and excludes multiples of 2, 3, and 5 from the trial factor pool.

After loading the program (which is 79 steps in length and uses registers 1, 2, and 3), enter the number to factor and press C to see the first factor. Then press R/S to see each additional factor. 0 indicates done.

Thanks to Dave Britten for making me aware of this very elegant algorithm.

Note: code modified on 1/12/2017 to remove unnecessary division, saved 2 bytes.

Note: code modified on 1/26/2017 to remove two unnecessary LN's, saves 7 seconds execution time to find that 127,031 is prime

Code:
 LBL A RCL 1 + STO 1 RCL 2 X<-->Y / FRAC 0 X=/=Y RTN NOP LST X STO 2 SQRT STO 3 RCL 1 R/S 0 GTO A LBL B 4 A 2 A 4 A 2 A 4 A 6 A 2 A 6 A RCL 2 1 X=Y 0 RTN RCL 1 RCL 3 X>Y GTO B RCL 2 R/S CLX RTN LBL C STO 2 SQRT STO 3 0 STO 1 2 A 1 A 2 A 2 A RCL 2 1 X=/=Y GTO B 0 RTN
11-06-2014, 08:04 PM
Post: #2
 Dave Britten Senior Member Posts: 1,005 Joined: Dec 2013
RE: HP-65 prime factor finder
Very nice. Did you have to change much to get it running on the 65? I see those NOPs in there; that's because of the behavior of conditionals with respect to the unmerged GOTO, right?

I should do a quick conversion for my TI-58C so we can all have a good laugh at how much slower it no doubt is.
12-05-2014, 03:21 AM (This post was last modified: 12-05-2014 03:56 AM by Don Shepherd.)
Post: #3
 Don Shepherd Senior Member Posts: 569 Joined: Dec 2013
RE: HP-65 prime factor finder
(11-06-2014 08:04 PM)Dave Britten Wrote:  Very nice. Did you have to change much to get it running on the 65? I see those NOPs in there; that's because of the behavior of conditionals with respect to the unmerged GOTO, right?

I should do a quick conversion for my TI-58C so we can all have a good laugh at how much slower it no doubt is.

Hi Dave, sorry for a belated reply, I haven't checked this sub-forum recently.

It was a very easy port of your original code to the 65. The 65 has just the right amount of resources to support this program.

Yes, NOP's are required if what you want to "do if true" is only one byte long, since it skips two bytes assuming you usually want to do a GOTO which requires 2 bytes.

I love that old 65. I just hope I die before it finally does, it would just break my heart!

I'm going to try to implement this algorithm on a Tandy model 102 portable computer I recently got from a forum member. That will be an interesting exercise, to go from keystroke to BASIC!
12-05-2014, 03:43 AM
Post: #4
 Gene Moderator Posts: 858 Joined: Dec 2013
RE: HP-65 prime factor finder
(11-06-2014 08:04 PM)Dave Britten Wrote:  Very nice. Did you have to change much to get it running on the 65? I see those NOPs in there; that's because of the behavior of conditionals with respect to the unmerged GOTO, right?

Gene: Yes. Unmerged = skip 2 steps after a conditional. Fun!
12-05-2014, 12:27 PM (This post was last modified: 12-05-2014 02:53 PM by Dave Britten.)
Post: #5
 Dave Britten Senior Member Posts: 1,005 Joined: Dec 2013
RE: HP-65 prime factor finder
(12-05-2014 03:21 AM)Don Shepherd Wrote:  I'm going to try to implement this algorithm on a Tandy model 102 portable computer I recently got from a forum member. That will be an interesting exercise, to go from keystroke to BASIC!

I think I did that a few months ago, actually. It was admirably speedy, as I recall. It was pretty much identical to the GW-BASIC version I did for my Omnibook and palmtops. I need to build a cassette cable for my 102 (and CoCo 2), but it seems like the Radio Shacks around here have stopped carrying 5-pin DIN connectors.

EDIT: Here's my GW-BASIC version if you're curious. I can't remember if the timer stuff is the same on the 102's version of BASIC, but it's extremely similar to GW-BASIC. Other than that, it would probably run as-is. Maybe I'll have to pop some batteries into it this weekend and race it against my 200LX.

Factor.bas
12-05-2014, 03:48 PM (This post was last modified: 12-05-2014 03:51 PM by Don Shepherd.)
Post: #6
 Don Shepherd Senior Member Posts: 569 Joined: Dec 2013
RE: HP-65 prime factor finder
(12-05-2014 12:27 PM)Dave Britten Wrote:
(12-05-2014 03:21 AM)Don Shepherd Wrote:  I'm going to try to implement this algorithm on a Tandy model 102 portable computer I recently got from a forum member. That will be an interesting exercise, to go from keystroke to BASIC!

I think I did that a few months ago, actually. It was admirably speedy, as I recall. It was pretty much identical to the GW-BASIC version I did for my Omnibook and palmtops. I need to build a cassette cable for my 102 (and CoCo 2), but it seems like the Radio Shacks around here have stopped carrying 5-pin DIN connectors.

EDIT: Here's my GW-BASIC version if you're curious. I can't remember if the timer stuff is the same on the 102's version of BASIC, but it's extremely similar to GW-BASIC. Other than that, it would probably run as-is. Maybe I'll have to pop some batteries into it this weekend and race it against my 200LX.

Factor.bas

Thanks for that link, Dave, I just printed the program. I'll implement that on my 102 after school today. Bill/Smithville NJ graciously gave me a 102 he acquired at a yard sale. My students say it looks "old" and I replied "it IS old!". I always wanted to get one of these, it is a very nice platform for BASIC programs, and I REALLY like the fact you don't have to wait 5 minutes after turning it on to do something productive, like you-know-what-OS (apologies to Bill Gates, who helped write the BASIC on the 102).

I'm living in the past with my Tandy 102, HP-65, BASIC programming, Wonder Years DVDs, and Pulsar watch! And lovin it!
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)