[HP35s] Program for prime number (Wheel Sieve and Miller-Rabin)
|
02-11-2019, 05:16 PM
(This post was last modified: 04-12-2019 12:44 PM by fred_76.)
Post: #1
|
|||
|
|||
[HP35s] Program for prime number (Wheel Sieve and Miller-Rabin)
Hello
My name is Fred, I am from France. I am used to HP calculators since I was young in the 80's. My father was working in a military-like industry and was supplied with new HP calc every two or three years. Therefore I was always fed with the previous calc he had. It all started to me with the grey HP65, then the HP67, the HP34, the HP41 (don't remember which one exactly). I don't have them anymore, they were trashed ... sigh ... I bought a HP32S and that was one of the very best I had. It was stollen during a company convention... I then asked the company to buy me a calc, it was a HP48S, again it was stollen when my office was broken ! The company bought me another calc, a HP48GII that I found desesperatly anoying and hard to program. My sons just offered me a HP35S and I rediscovered the pleasure of the RPN calculators. Here is a code that I find quite effective to determine if a number is prime or not, and that also calculates the factors. It make an extensive use of the stack to avoid unecessary STO/RCLs. It also makes use of fast tricks, such as storing repetitive constants and using included operations. For example [6] [+] is about 4.3 times slower than [RCL+S] (with 6 previously stored in S). The logic below is a brut force with elimination of the trivial factors that are multiples of 2, 3 and 5. From 7, you just loop by adding 4,2,4,2,4,6,2,6 to the trial factor. Here is the code : Code:
To use it, just enter the number to test, then press XEQ P.
It runs quite fast on my HP35S : PHP Code: Digits Prime Number Total Time All the best Fred --- edit : added time for 12 digits --- edit : added lines P089/P090 to handle exceptions 2/3/5/7 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 8 Guest(s)