(12C) Prime Factorization - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (12C) Prime Factorization (/thread-14035.html) |
(12C) Prime Factorization - Don Shepherd - 11-23-2019 03:04 AM Well, Dave Britten started this ball rolling here with a prime factor finder for the 32sii and 20s based upon an original program for the 67. Tim Wessman used this as a model for the 30b in message number 30 of that thread. I made a few modifications here for the 32s, a very nice machine. But I always wondered if this basic algorithm could be done on the 12c, and especially the fast 12c+. At first glance, it would appear unlikely because of the heavy reliance on subroutines, which the 12c does not have. But the more I looked at the code, the more I believed that it should be possible to somehow implement this algorithm on the 12c+. Yesterday I figured out how to do it, essentially using indirect addressing via the Rcl CFj command with the cash flow registers. The code is listed below. The 12c+ is rather slower than the 30b for this application: the 30b determines the primality of 300,000,007 in 9 seconds versus the 12c+ 38 seconds. But this algorithm represents a significant improvement over a brute-force algorithm that eliminates only multiples of 2 from the trial factor pool; that version takes 57 seconds on the 12c+. The fact that an algorithm that is so subroutine-intensive can be done on the 12c+ at all is a testament to the greatness of the design of the 12c. No wonder it is the most successful calculator HP has ever produced. Edited on 9/20/2010 to implement Katie Wasserman's suggestion to use Nj in addition to CFj so that no preloading of registers is necessary, a brilliant suggestion. Prime factors program for 12c+ Eliminates multiples of 2, 3, and 5 from trial factor pool These are the trial factor increment values that are stored via cfj and nj in R2 to R7: 6,2,6,4,2,4,2,4,2,2,1,2 Enter number to factor, R/S R/S after each factor displayed 0 indicates done maximum number to factor = 999,999,999 Register usage: R2 - R7 (and corresponding nj) = trial factor increments, begins at R7 R0 - number to factor R1 - current trial factor n - used to control indirect addressing i - flag for returning to the right location from the routine at line 56 Mem command = p-71 r-11 Code:
RE: (12C) Prime Factorization - Paul Dale - 11-23-2019 03:23 AM Couldn't the final two steps be replaced by goto 57? Pauli RE: (12C) Prime Factorization - Don Shepherd - 11-23-2019 03:45 AM (11-23-2019 03:23 AM)Paul Dale Wrote: Couldn't the final two steps be replaced by goto 57? Hi Pauli, thanks. It looks like that change would work, but the 2010 post is archived and cannot be changed so I'd have to do some cutting and pasting into a new post, which I'd rather not do. There are three additional changes I wanted to make back in 2010 (to reduce the number of indirect registers by 1) but I couldn't make the changes because of the archive status. Those changes didn't really affect the speed or functionality of the program anyhow. UPDATE: I'm going to remove the link to the archived article (that can't be updated) and post the revised code in its place--to include Pauli's suggestion and also use one fewer register for indirect addressing. Don |