Carmichael 3 strong pseudoprimes: Difference between revisions

Shorer D entry
m (Less imports in D entry)
(Shorer D entry)
Line 28:
=={{header|D}}==
From the Python entry, with several changes. Imports the third (extensible) D entry of the Sieve of Eratosthenes Task.
<lang d>import std.stdio, std.typecons, sieve_of_eratosthenes3;
 
struct Carmichael {
static struct Tri {immutable int x, y, zp1; }
immutable int p1;
 
static int mod(in int n, in int m) pure nothrow {
return ((n % m) + m) % m;
}
 
int opApply(immutable int delegate(in ref TriTuple!(int,int,int)) dg) {
int result;
 
if (IsPrime(p1))
foreach (immutable h3; 2 .. p1) {
immutable g = h3 + p1;
foreach (immutable d; 1 .. g)
if ((g * (p1 - 1)) % d == 0 && mod(-p1 * p1, h3) == d % h3) {
immutable p2 = 1 + && mod(-(p1 * p1,- h31) ==* dg %/ h3d) {;
immutable p2 = 1 +if (IsPrime(p1 - 1p2)) * g / d);{
immutable p3 = 1 + (p1 * p2 / if (IsPrime(p2h3)) {;
if (IsPrime(p3) && (p2 immutable* p3) = 1 +% (p1 *- p21) /== h31); {
// A int[3] literal if (IsPrime(p3))heap-allocates.
const tri = if Tuple!((p2 * p3int,int,int) % (p1, -p2, 1p3) == 1) {;
result = // A int[3] literal heap-allocates.dg(tri);
if auto triple = Tri(p1,result) p2, p3)break;
result = dg(triple);}
if (result) break;
}
}
}
}
}
}
 
return result;
}
}
 
void main() {
foreach (immutable n; 0 .. 62)
if (IsPrime(n))
foreach (const c; Carmichael(n))
writefln("%(%d x %)", [c.tupleof]);
}</lang>
{{out}}