Lucas-Lehmer test: Difference between revisions
PARI/GP →Version with trial division and fast modular reduction: improved trial division
Hendursaga (talk | contribs) m (Fix minor grammar errors.) |
(PARI/GP →Version with trial division and fast modular reduction: improved trial division) |
||
Line 2,337:
ll.gp
<syntaxhighlight lang="
/* ll(p): input odd prime 'p'. */
/* returns '1' if 2^p-1 is a Mersenne prime. */
ll(p) = { ▼
my(l=log(p), ld=log(l));
▲ll(p) =
forprimestep(q = 1, sqr(ld)^(l/log(2))\4, p+p,
if(Mod(2,q)^p == 1, return)
);▼
▲ /* do some trial division up to a reasonable depth. */
/* Lucas-Lehmer test with fast modular reduction. */ ▼
s = sqr(s);
if(s >= m, s -= n, s -= 2)▼
);▼
!s▼
▲ /* Lucas-Lehmer test with fast modular reduction. */
for(i = 3, p,▼
s = sqr(s);▼
▲ s = bitand(s,m)+s >> p;
▲ if(s >= m, s -= n, s -= 2)
▲ );
▲ !s
▲}; /* end ll */</syntaxhighlight>
/* get Mersenne primes in range [a,b] */
llrun(a, b) = {
my(t=0, c=1, p=2, thr=default(nbthreads));▼
printf("#%d\tM%d\t%3dh, %2dmin, %2d,%03d ms\n", c, p, t\3600000, t\60000%60, t\1000%60, t%1000);▼
);▼
if(d,▼
c++;
)
)
}; /* end llrun */
<syntaxhighlight lang="parigp">▼
\\ export(ll);
Compiled with gp2c option: gp2c-run -g ll.gp.▼
<syntaxhighlight lang="c">llrun(2, 132049)</syntaxhighlight>
▲ thr=default(nbthreads);
▲ printf("#%d\tM%d\t%3dh, %2dmin, %2d,%03d ms\n", c, p, t\3600000, t\60000%60, t\1000%60, t%1000);
▲ parforprime(p=3, 132049, ll(p), d, /* ll(p) -> d copy from parallel world into real world. */
▲ if(d,
▲ c++;
▲ t += gettime()\thr;
▲ printf("#%d\tM%d\t%3dh, %2dmin, %2d,%03d ms\n", c, p, t\3600000, t\60000%60, t\1000%60, t%1000)
▲ )
▲ )
{{out}}
▲Compiled with gp2c option: gp2c-run -g ll.gp.
Done on Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz, 4 hyperthreaded cores.
Line 2,394 ⟶ 2,395:
#11 M107 0h, 0min, 0,000 ms
#12 M127 0h, 0min, 0,000 ms
#13
#14
#15 M1279 0h, 0min, 0,
#16 M2203 0h, 0min, 0,
#17 M2281 0h, 0min, 0,
#18 M3217 0h, 0min, 0,
#19 M4253 0h, 0min, 0,
#20 M4423 0h, 0min, 0,
#21 M9689 0h, 0min, 1,
#22 M9941 0h, 0min, 2,
#23 M11213 0h, 0min,
#24 M19937 0h, 0min,
#25 M21701 0h, 0min,
#26 M23209 0h, 0min,
#27 M44497 0h,
#28 M86243 1h,
#29 M110503
#30 M132049
? ##
*** last result: cpu time
=={{header|Pascal}}==
|