Lucas-Lehmer test: Difference between revisions

Updated D entry
(→‎{{header|Python}}: loop without division)
(Updated D entry)
Line 419:
<lang d>import std.stdio, std.math, std.bigint;
 
bool isPrime(in int p) pure nothrow {
if (p < 2 || p % 2 == 0)
return p == 2;
foreach (immutable i; 3 .. cast(uint)(sqrt(cast(real)p)) + 1)
if (p % i == 0)
return false;
Line 428:
}
 
bool isMersennePrime(in int p) /*pure /*nothrow*/ {
if (!isPrime(p).isPrime)
return false;
if (p == 2)
return true;
autoimmutable mp = (1.BigInt(1) << p) - 1;
auto s = 4.BigInt(4);
foreach (immutable _; 3 .. p + 1)
s = (s ^^ 2 - 2) % mp;
return s == 0;
Line 441:
 
void main() {
foreach (immutable p; 2 .. 2_300)
if (isMersennePrime(p).isMersennePrime) {
write(" 'M"', p, ' ');
stdout.flush();
}
}</lang>
{{out}}
<pre> M2 M3 M5 M7 M13 M17 M19 M31 M61 M89 M107 M127 M521 M607 M1279 M2203 M2281 </pre>
With p up to 10_000 it prints:
<pre> M2 M3 M5 M7 M13 M17 M19 M31 M61 M89 M107 M127 M521 M607 M1279 M2203 M2281 M3217 M4253 M4423 M9941 </pre>
 
=={{header|DWScript}}==