(29C) Prime numbers up to 10'000
|
09-04-2018, 06:53 PM
(This post was last modified: 04-03-2022 05:57 PM by Thomas Klemm.)
Post: #10
|
|||
|
|||
RE: (29C) Prime numbers up to 10'000
(09-04-2018 12:04 AM)Archilog Wrote: A guy called C.Ret did an awesome work which can be found there: Silicium Indeed it's amazing. Here's the flow-chart: And here's the program for the HP-19C: Quote:Pour imprimer les nombres premiers jusqu'à m (m compris entre de 50 et11449): This took me a while to figure out. Here's how to initialise the registers, say for m = 10,000. 4 STO 1 ; 4 instead of 5 10000 STO 2 7 STO 3 ; STO 3 instead of STO 1 200 STO 4 Otherwise register 1 is incremented in line 88 to 6 and then the heap starts there. But in line 29 it's hard-coded to use register 5. I'm currently running both programs side by side in the aforementioned emulator. His program is happily outrunning my program. Here's a listing for those who want to run it: Code: 01: 15 13 00 : LBL 0 ; Minimal initialization What's so cool about it? Instead of checking if a number is divisible by a prime the odd multiples of the primes are calculated starting from the square of the prime. For 7 that would be: 49, 63, 77, 91, 105, … Or for 11 that would be: 121, 143, 165, 187, … Of course we can't keep all of them since we have only 30 registers. But we don't have to. All we need is keeping the smallest among them. These numbers are kept in a min-heap and thus we only have to check the smallest of them which is in register 5. If the number is smaller it is a prime. If they are the same it's not a prime and we can proceed with the next number. If it is bigger we have to update the multiples of primes in the min-heap. And while doing so make sure it's still a min-heap. Thus some of the elements have to be rearranged. The prime is added as a decimal value to the multiples: For 7 that would be: 49.07, 63.07, 77.07, 91.07, 105.07, … This allows to calculate the next number. We can start with 7 since multiples of 2, 3 and 5 are omitted from the list of numbers. The main loop with the multiple GSB commands generates sequence A007775 (apart from 1 of course). I don't have a full understanding of the code for the min-heap yet. Thus if C.Ret is reading this post I'm eager to get some explanations from the master himself. Kind regards Thomas PS: Meanwhile we're at 1777 vs. 2137. The 2nd is of course C.Ret's program. Edit: Added comments and stack diagrams to the listing of the program. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)