[HP35s] Program for prime number (Wheel Sieve and Miller-Rabin)
|
04-13-2019, 06:24 PM
(This post was last modified: 10-09-2019 11:08 PM by Albert Chan.)
Post: #54
|
|||
|
|||
RE: [HP35s] Program for prime number (Wheel Sieve and Miller-Rabin)
(04-11-2019 03:18 PM)fred_76 Wrote: Modular multiplication MOD function is computational expensive. You can rearrange the formula to reduce MOD count per multiply from 4 to 3 A*B = (xk + y)*(uk − v) = (xk uk) − (y v) + (y uk − v xk) Base number must be powers of 10, otherwise MOD won't work. Similarly, IEEE double must pick 2^26 as base, sqrt(2^53-1) ~ 94906266 won't work. Since (2^26)^2 < 2^53-1, some adjustment is needed for huge IEEE double. This is my mulmod(a, b, n) Lua code: PHP Code: local function mulmod(a, b, n) -- a*b (mod n) Quote:Prime routine To cover *full* 53 bits IEEE double range, I use above link with 5 best bases. However, that does not quite reach 53 bits. One simple check pushed it over 53 bits Snippet from my isprime(n) PHP Code: -- Below good for full 53 bits (2^53-1 = 9007199254740991). Note that the huge numbers are represented *exactly* in binary. Also, adjustment (-4,-3,-291) must be negative, to avoid 53-bits overflow. lua> p = require 'nextprime' lua> x = 2^53-1 lua> for i=1,10 do x=p.prevprime(x); print(x) end 9007199254740881 9007199254740847 9007199254740761 9007199254740727 9007199254740677 9007199254740653 9007199254740649 9007199254740623 9007199254740613 9007199254740571 Edit: nextprime.lua in https://github.com/achan001/PrimePi |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 12 Guest(s)