(48 42s) 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: (48 42s) prime factorization (/thread-22806.html) |
(48 42s) prime factorization - Dirk E. - 12-04-2024 08:06 PM Put a natural number 1 < n < 1.E12 on the stack. (Avoid other values or types.) The program returns it's prime factorization as an algebraic expression - or the number itself, if it's a prime. EVAL brings back n. Display mode STD. The Argument 'n' (or Remainder 'r' after factors found) and the next factor 'x' to try are kept directly on the stack. 'c' is compared to 'x' as terminating condition or counts how often a factor occurs. The result is collected in 'f'. Program variant PC2 (below) needs less storage, but is slower; another PC3 is between them. Sizes of .hp objects stored by Emu48: PF1 570, PC2 448, PC3 485 Bytes. PF1 Code:
PC2 Code:
PC3 Code:
For testing how long a program runs put the argument(s) on the stack, then the program name, then run TTST. (Ideally in the same directory.) Note: TTST resets the Display mode to STD. E.g. 99999989 ENTER 'PC3' ENTER TTST --> "PC3 0.0037774" (i.e. 37.8 sec). TTST Code:
Results with Emu48 set to Authentic Calculator speed (h.mmss(.)sss): Code:
Accordingly PF1 should run about 4:32 min on 9999999967 or 45:13 min on 999999999989. If not set to Authentic speed, it runs on a mobile Emu48 57.3 sec on 999999999989 (arm64 8x2GHz with powersaver on, 33 s on a desktop). On the mobile Free42 takes roughly 3s with 999999999989 runing the following 42s prime factorization program in RPN (normally it shows the remainder in Y: and one factor at a time in X:, continues by R/S; it's finished when Y: shows 1 - using X Y Z T and L): Code:
For Free42 the attached .raw.hp may need to be renamed back to .raw. (After I had learned about a mod 30 loop I had thought about writing one for 42s, but then I decided to remain with mod 6 since everything else would mean much more typing - the disadvantage of my approach here is that the factors need to be noted manually - or the R/S replaced by PRX e.g. Later I took it as learning exercise for User RPL and moved to '48 with my factorization project; quite challenging, because a subroutine is not 'jumped' to, but loaded onto the stack and then 'evaluated'. Without a subroutine - PF1 - indeed is faster on the long run, but it needs more space in memory.) Since actually Free42 works with 34 digits, it's possible to check higher than 1.E12 values - running time will grow, of course, by factor 10 for each two more places. On the 48 the binary mode would also allow 64 bits (18 decimal places), but the condition DUP2 MOD NOT in real arithmetic would become DUP2 DUP2 / * - # 0d == much longer and slower... Many thanks to all who made Emu48 and Free42 possible -- happy computing! D. :-) |