(27S,17B,..) FACTOR revisited
|
06-15-2022, 03:07 PM
Post: #1
|
|||
|
|||
(27S,17B,..) FACTOR revisited
In the 'Step-by-Step Solutions' - Technical Applications manual for the HP-27S and HP-19B, on page 52 is an equation to find all prime factors of a number:
Code: FACTOR:FACT=0xL(E:N)+ This equation is quite slow: it loops over every odd number, comparing it to 8 numbers in each loop. It is better simplified like this, testing only 2 out of three odd numbers: (it can be improved still factoring out the 5's, making the equation quite a bit larger) Code: FACTR1:FACT=0xL(E:N)x Still, it takes ages for large numbers, even to find a small factor, because it must always do the full loop 5..SQRT(N). Since we cannot exit a loop (save with an error, but then execution stops altogether), I tried to find a way to speed this up. In the following equation, I have converted the inner loop into two nested loops. The innermost loop doubles in size each iteration, and is skipped if a factor has been found in the meantime. Code: FACTR2:FACT=0xL(E:N)x seems to work ;-) BTW it also works in Plus42, but there the speed is not really an issue ;-) Let's look at the speed improvements. Bear in mind that the original FACTOR is still slower than FACTR1 Code: take 9999997 = 7*1428571 the value between brackets is an estimate So, to find small factors is many times faster, as the factors grow that difference becomes smaller, and there's a slight overhead for the last factor, or when N is prime to begin with. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(27S,17B,..) FACTOR revisited - Werner - 06-15-2022 03:07 PM
|
User(s) browsing this thread: 2 Guest(s)