(34S) Prime Factors
|
05-30-2014, 01:53 AM
(This post was last modified: 06-15-2017 01:21 PM by Gene.)
Post: #1
|
|||
|
|||
(34S) Prime Factors
Got my WP-34S fully built today, so naturally, that means you're getting the usual prime factoring algorithm that I've been dragging along to absolutely everything since I found it in that HP-67 program listing.
This one adds a few enhancements: factors are all obtained up front as quickly as possible, and then a simple menu is displayed allowing the user to view the list with the Up and Down arrow keys. Usage: Enter number to factor into X, XEQ 'FAC'. Once all factors are found, use the up and down arrows to review the results. It's pretty quick; factoring 167,699,497 down to 7 * 3,851 * 6,221 takes around 2.5 seconds. BUG FIX EDIT: Swapped steps 88 and 89 to prevent the VWa+ from disappearing when PSE 99 times out. Code: 001 LBL'FAC' LBL 'FAC', LBL 01, and LBL 03 are all just the standard prime factoring routine. LBL 09 sets up the result viewer, LBL 10 displays the currently selected result, and LBL 11 handles the keyboard for viewing other results. CAUTION: The factors are stored in global registers starting with 01, and going up to as many as are necessary to hold all factors. This is to allow for use of the interactive factor viewer. Suggestions and improvements are welcome. This is my first crack at a WP-34S program. |
|||
05-30-2014, 03:30 AM
Post: #2
|
|||
|
|||
RE: (WP-34S) Prime Factors
You could use NEXTP to find the next prime.
Cheers Thomas |
|||
05-30-2014, 03:54 AM
Post: #3
|
|||
|
|||
RE: (WP-34S) Prime Factors
(05-30-2014 03:30 AM)Thomas Klemm Wrote: You could use NEXTP to find the next prime. Now that's interesting. I just replaced the MOD 30 loop with NEXTP to get the next candidate factor, and it took about 10 times as long to factor 167,699,497 - about 20 seconds. Knowing the amount of work NEXTP probably has to do with each call, I guess that's not a huge surprise. |
|||
05-30-2014, 08:24 AM
Post: #4
|
|||
|
|||
RE: (WP-34S) Prime Factors
(05-30-2014 03:54 AM)Dave Britten Wrote:(05-30-2014 03:30 AM)Thomas Klemm Wrote: You could use NEXTP to find the next prime. After a 2nd thought: that was maybe a bad idea. Well, it depends on how NEXTP is implemented. But then have a look at the code of PF: I was not the first one to come up with this idea. Just tested 167,699,497 on the iPhone with PF and it was reasonably fast: maybe a second to find the factor 3,851. Is the iPhone so much faster than the original WP-34s? Cheers Thomas |
|||
05-30-2014, 07:06 PM
Post: #5
|
|||
|
|||
RE: (WP-34S) Prime Factors
(05-30-2014 08:24 AM)Thomas Klemm Wrote: Well, it depends on how NEXTP is implemented. PRIME? Code: /* Test if a number is prime or not using a Miller-Rabin test */ NEXTP Code: /* Very minimal routine to return the next prime in sequence |
|||
05-30-2014, 08:07 PM
Post: #6
|
|||
|
|||
RE: (WP-34S) Prime Factors
My hunch is that repeatedly calling NEXTP like that causes a ton of duplicated work. I don't imagine there's any sophisticated caching going on with the limited RAM in the 30b hardware. (See: Schlemiel the Painter)
If that's the case, using it in a prime factoring algorithm is probably taking it to O(N * log N) complexity at best as the input argument grows, whereas the MOD 30 loop keeps it O(N) (even if it's not as fully optimal as it could be). |
|||
05-30-2014, 08:58 PM
Post: #7
|
|||
|
|||
RE: (WP-34S) Prime Factors
(05-30-2014 08:07 PM)Dave Britten Wrote: My hunch is that repeatedly calling NEXTP like that causes a ton of duplicated work. I don't imagine there's any sophisticated caching going on with the limited RAM in the 30b hardware. (See: Schlemiel the Painter) The important thing is: we don't need to know whether a factor is prime or not to figure out if it divides the number. What ever the complexity of PRIME? may be (O(k log3n) according to Wikipedia), it can't be faster than a simple division. Thus if NEXTP has to test all the next odd numbers we don't gain anything. Using the MOD 30 loop you even gain a factor 1.875 compared to the minimal routine using only odd numbers. How does your program compare to the built-in program PF? Kind regards Thomas |
|||
05-30-2014, 09:42 PM
Post: #8
|
|||
|
|||
RE: (WP-34S) Prime Factors
(05-30-2014 08:58 PM)Thomas Klemm Wrote: The important thing is: we don't need to know whether a factor is prime or not to figure out if it divides the number. What ever the complexity of PRIME? may be (O(k log3n) according to Wikipedia), it can't be faster than a simple division. Thus if NEXTP has to test all the next odd numbers we don't gain anything. Where is PF hiding? I don't see that in the catalogs or the manual. |
|||
05-30-2014, 10:57 PM
Post: #9
|
|||
|
|||
RE: (WP-34S) Prime Factors | |||
05-30-2014, 11:58 PM
(This post was last modified: 05-31-2014 12:46 AM by Dave Britten.)
Post: #10
|
|||
|
|||
RE: (WP-34S) Prime Factors
(05-30-2014 10:57 PM)Thomas Klemm Wrote:(05-30-2014 09:42 PM)Dave Britten Wrote: Where is PF hiding? I don't see that in the catalogs or the manual. I get "No such label" if I try to XEQ it. I'm running 3.2 3472 on a 30b. All I see in Lib are the couple of programs I put there with PSTO. EDIT: Oh, I see what's going on. The iOS version has a larger "flash" memory, and a few preloaded programs as a result. I'll see if I can key in PF on my 'real' unit and speed test it a bit. |
|||
05-31-2014, 01:09 AM
Post: #11
|
|||
|
|||
RE: (WP-34S) Prime Factors
Alright, the included PF is just using the NEXTP approach with a simple square-root bailout, so it's about as slow to reach the second factor as when I attempted it with NEXTP. It's using a few BACKs/SKIPs to optimize branching, but I don't think that buys it much. It IS only 29 steps, though, so it's pretty space-efficient.
So whoever maintains the iOS version of the emulator, if you're reading this, feel free to replace PF with FAC. Not only is it faster, it's also a nice demo of KEY? to browse stored data. I think next I'll experiment with using LocR-> to dynamically allocate local regs to avoid overwriting globals. That'll make the factor browser more complicated, since it won't be starting at reg 01, though (but that's assuming redefining LocR even works in the first place). |
|||
06-01-2014, 06:11 PM
Post: #12
|
|||
|
|||
RE: (WP-34S) Prime Factors
So this is funny: if you replace the NEXTP call in PF with a simple INC X, the time to find the second factor of 167,699,497 drops from 22 seconds to about 5 or 6. I didn't bother getting fancier than that, because I'd have to update a bunch of SKIPs/BACKs.
|
|||
02-23-2015, 08:02 PM
(This post was last modified: 02-23-2015 08:22 PM by Dirk E..)
Post: #13
|
|||
|
|||
RE: (WP-34S) Prime Factors - performance challenge
With the subroutine-driven MOD30 loop shown here I felt challenged to find some optimal between speed, resources used and rubbish left on the SBR-stack, but it's quite hard to decide...
From looking through the 'PF' in the library I decided to remain with the RPN stack registers. Since the WP 34S offers so much enhanced TEST commands, you just have to remember, when the stack lifts or drops and when not. It reminded me a little an ancient predecessor of the Rubik's cube called "Schiebefax" in german - 4x4 squares less one in a square frame which were to be put into a certain order. Here's the very shortest (but also slowest) I found: Code: WP 34S (3.3 3678) 13.Feb.2015 Adding one line to the loop and a few for filtering out the even the required time lowers by a third: Code: WP 34S (3.3 3678) 13.Feb.2015 Still using only x, y and L, but leaving out divisions by multiples of 3, even more speed is gained, but on the cost of almost doubled line count: Code: WP 34S (3.3 3678) 13.Feb.2015 Now I became curious whether the MOD 30 algorithm, i.e. leaving out the multiples of 5, too, would be worth the effort. Formerly I was always convinced, the overhead to organize the loop would eat up the speed. Now I tried - and found the way that Dave presented here is definitely the fastest. Here it is, adapted to use the RPN stack x, y, z, t and L only. Since for a faster end condition test the root of the 'unknown' is also maintained and integer division employed, it uses a few lines and registers more: Code: WP 34S (3.3 3678) 17.Feb.2015 The time used by the equivalent routine, using global registers instead, is the same. While testing the speed I usually terminated after the second factor and started over from the beginning - soon I found out that the WP 34S keeps it's subroutine stack, differently from HP 35s and HP 42s that purge it upon a manual XEQ, GTO or OFF. Thus I thought it would be preferrable to avoid subroutines during which the program could be interrupted and terminated - I tried to use indirect addressing for a very short subroutine returning just the step between the division 'candidates' 1 - 2 - 2 - 4 - 2 - 4 - 2 - 4 - 6 - 2 - 6 (then continuing repeating the 8 elements starting from the first '4'). And I tried another way doing just an indirect branch, since the step value would be needed always at the same position in the main loop. Both were much slower than the following MOD 6 approach, that saves some time by employing integer arithmetics and keeping the root for the end test: Code: WP 34S (3.3 3678) 17.Feb.2015 This is almost as fast as the MOD 30 above and even 5 lines shorter. It also demonstrates that despite the main loop running one line more than the MOD 6 program with RPN stack x, y, L only cited above, the integer arithmetics RMDR and comparison with an integer gain a measurable amount of time. For the use of indirectly addressed local registers can be reviewed here, I'd like to show here my experiment using another indirect addressing for the factor chain step. I found it not too clear in the manual, but I understand that the global registers are indirect 00-99, the 12 stack and helper registers indirect 100-111, and local registers starting .00 become indirect 112... Here it is, the local registers are released before showing a factor that was found, then re-allocated; anyway, if the program is interrupted, they'd need to be freed manually: Code: WP 34S (3.3 3678) 8.Feb.2015 Maybe, this reply is a bit late, but I received my WP 34S only these weeks, and it was also my first program to do my own prime factorization - thank you for the interesting challenge with the 'PF' and the MOD 30 extention! Hopefully someone finds something interesting in it, too! On my somewhat slower android mobile on free42 I started exploring the WP34S's integer space and factorized 2^50+3 = 7 x 3 435 997 x 46 811 113. This ran about 3 minutes(!) till the second factor on the mobile, while the test 7 x 38447 x 62119 ran rather 3 seconds on it. Thus I'd be a little afraid running out of batteries or patience factorizing any double precision integer on the WP 34S. The 'PF' from the library ran more than 5 minutes factorizing 33013x33013 when I lost patience and stopped it before it found the first factor... Cheers! Dirk |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)