(35S) - prime factors
|
11-20-2014, 02:28 PM
(This post was last modified: 06-15-2017 01:33 PM by Gene.)
Post: #1
|
|||
|
|||
(35S) - prime factors
Since I learned from the 35s bug list how easy it is to loose all programs, I think shorter is better.
This program doesn't preserve the RPN stack, but therefore it's faster to enter and needs less other registers. There are some really nice procedures around here in the forum to save and restore the stack, and one can build a factors list by replacing the result display stops. The last factor may be displayed (rather redundantly) as "1", this happens because I left only one test for the exit condition to make the main loop shorter. My HP 35s found the second factor of 7 x 3851 x 6221 after 1:37 min. There are three loops - the first checking factors 2 and 3, then the second for factor 5, the third 7, the second again 11, the third 13 and so on... Of course the user should know himself that the whole works only for natural numbers which fit into the HP 35s' 12 digits. HP 35s - Prime factorization - test 7x3851x6221 second factor after 1'37" - usage: nnn XEQ P002 - 0 pf1 - R/S - ... 1 pfN - global registers, MOD 6 loop (7.Feb.2015 - RPN ln=143 ck=6626) X: 'unknown', T: factor to test, RPN stack ================================================================================ P001 LBL P P002 STO X P003 2 ;loop P004..P017 - Test 2 and 3 as factors P004 STO T P005 RCL X P006 RCL T P007 RMDR P008 x!=0? P009 GTO P014 P010 RCL T P011 STO/ X P012 STOP ;y=0, x='T' P013 GTO P005 ;try factor again P014 RCL T P015 3 P016 x!=y? P017 GTO P004 P018 STO/ T ;'T':=1 ;loop P019..P035 - 15(17) lines per 2(6) i.e. 75(85) per 10(30) test divides P019 4 P020 STO+ T ;'T':=6n-1 P021 RCL X P022 RCL T P023 RMDR P024 X=0? P025 GTO P040 P026 2 P027 STO+ T ;'T':=6n+1 P028 RCL T P029 RCL X P030 RCL/ T P031 x<y? ;'X/T'<'T' or 'X'<'T'^2 P032 GTO P044 ;-> End P033 FP P034 x!=0? P035 GTO P019 ;-> next factor P036 RCL T P037 STO/ X P038 STOP ;y=0, x='T' P039 GTO P028 ;try factor again P040 RCL T P041 STO/ X P042 STOP ;y=0, x='T' P043 GTO P021 ;try factor again P044 1 P045 RCL X P046 RTN ;y=1, x='X' Since I was always convinced that the control overhead would cost more than what you gain leaving out also test divisions by multiples of 5, I didn't really try before - when I got my first WP34S two weeks ago, I felt challenged to do it though. The main loop of the above will run 15 lines and skip two for each two factor candidate divisions. In the primer below I tried everything to gain speed - compare to the previously saved integer part of "X" ' square root should be faster than a complete float compare; also the three lines RCL X - RCL T - RMDR should run faster than RCL X - RCL/ T - FP. Anyway It seems that the use of ISG and the indirect register call in place of a constant eat up almost all of the advantage. (And I don't know whether the position in the memory makes a difference - maybe, I'd have to reset the calculator and enter just the one program for time comparisons?) The following program's main loop tests only 8 factors from a sequence of 30 natural numbers while the above tests 10, for those 8 it runs 69 and skips 9 lines while the above runs 75 and skips 10 lines for the whole 10 tests. This should give an advantage of 7%, but I took 1:34 min to find the second factor of 7 x 3851 x 6221, which is only 2% less and seems a rather marginal win for entering 18 more lines and using 13 more registers: Code: HP 35s - Prime factorization - test 7x3851x6221 second factor after 1'34" On the WP 34S I ran further tests, and found it preserves the subroutine stack almost as stable as the programs. Since my tests ended with the first or second factor, I found some trash left on the subroutine stack and consider it better to avoid subroutines if possible. A few tests with the HP 35s and the HP 42s showed that a manual GTO, XEQ or OFF clears the subroutine stack, thus on these models the MOD 30 routine that Dave presented will less mess up this stack. Tus to be able to compare the speed of XEQ/RTN against the RCL(I)/ISG used above, I tried the MOD 30 with subroutine way, too. It ran significantly faster, although the main loops run nearly the same number of lines. Code: HP 35s - Prime factorization - test 7x3851x6221 second factor after 1'18.7" When I looked through the WP 34S library 'PF' routine I felt challenged to do the factorization on the HP 35s without using any registers except the RPN stack, too. It's possible, but costs additional steps and thus time. The slight stack manipulation helped to keep it in reasonable size. Code: HP 35s - Prime factorization - test 7x3851x6221 second factor after 1'47" - The following seems to me showing the advantages of RPN quite well, and I think that it may fit here. Thanks to Euklid it's not too hard to add the greatest common divisor and least common multiple functions found on other recent calculators. Having a simple example in front of me made it somewhat easier. Using only x, y and LastX, it just doesn't return with the correct "LASTx". (LCM here needs one global Register, but saves the correct LASTx.) HP 35s - GCD (RPN: ck=87EC ln=30) User entry - output: nnnn1 ENTER nnnn2 XEC G - gcd(n1,n2) - uses: LASTx G001 LBL G G002 RMDR G003 x=0? G004 GTO G008 G005 LASTx G006 x><y G007 GTO G002 G008 CLx ;prevent automatic Stack Lift G009 LASTx G010 RTN HP 35s - LCM (RPN: ck=F8E6 ln=27) User entry - output: nnnn1 ENTER nnnn2 XEC L - lcm(n1,n2) - uses: GCD (above), X, G (gcd result) L001 LBL L L002 STO G L003 x><y L004 STO X L005 XEC G002 L006 STO/ X ;X := (previousY)/gcd L007 x>< G ;G := gcd L008 RCLx X ;LASTx := previous X L009 RTN Here's added a second approach using 2 storage registers (similiar to the above lcm) but providing the LASTx for gcd, too, also combining both under one label: Code: HP 35s - GCD, LCM (RPN: ck=3E22 ln=72) The bug list mentioned that checksum and length are pointless - my impression is, they are consistant as long the calculator remains in default state, maybe with display settings applied. Apparently there are internal flags stored invisibly if entering expressions in ALG mode, equations or fractional, complex, vector constants. And of course the checksum changes if a different Label is used. So my 35s remained in RPN, "RDX," and FIX 4 (although ALL might look nicer for integer calculations) - and you may compare these checksums to yours, if you put in just the same code. ...now turning to my WP3xS Dirk EDIT (7.Feb.2015): Reverted from a display Subroutine to separate Stops (6 more lines, though), added the indirect addressing exercise. (15.Feb.2015): added the MOD 30 with subroutine and the stack-only study. _______________ Sanyo CZ1210, TI-66, fx-4500P, HP 15C LE, fx-5800P, HP 35s, WP 34S ...? |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)