Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Emirp-primes/D/emirp-primes-1.d
Normal file
34
Task/Emirp-primes/D/emirp-primes-1.d
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
bool isEmirp(uint n) pure nothrow @nogc {
|
||||
bool isPrime(in uint n) pure nothrow @nogc {
|
||||
if (n == 2 || n == 3)
|
||||
return true;
|
||||
else if (n < 2 || n % 2 == 0 || n % 3 == 0)
|
||||
return false;
|
||||
for (uint div = 5, inc = 2; div ^^ 2 <= n;
|
||||
div += inc, inc = 6 - inc)
|
||||
if (n % div == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint reverse(uint n) pure nothrow @nogc {
|
||||
uint r;
|
||||
for (r = 0; n; n /= 10)
|
||||
r = r * 10 + (n % 10);
|
||||
return r;
|
||||
}
|
||||
|
||||
immutable r = reverse(n);
|
||||
return r != n && isPrime(n) && isPrime(r);
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
auto uints = uint.max.iota;
|
||||
writeln("First 20:\n", uints.filter!isEmirp.take(20));
|
||||
writeln("Between 7700 and 8000:\n",
|
||||
iota(7_700, 8_001).filter!isEmirp);
|
||||
writeln("10000th: ", uints.filter!isEmirp.drop(9_999).front);
|
||||
}
|
||||
43
Task/Emirp-primes/D/emirp-primes-2.d
Normal file
43
Task/Emirp-primes/D/emirp-primes-2.d
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import std.stdio, std.algorithm, std.range, std.bitmanip;
|
||||
|
||||
/// Not extendible Sieve of Eratosthenes.
|
||||
BitArray sieve(in uint n) pure nothrow /*@safe*/ {
|
||||
BitArray composites;
|
||||
composites.init([true, true]);
|
||||
composites.length = n;
|
||||
if (n < 2)
|
||||
return composites;
|
||||
|
||||
foreach (immutable uint i; 2 .. cast(uint)(n ^^ 0.5) + 1)
|
||||
if (!composites[i])
|
||||
for (uint k = i * i; k < n; k += i)
|
||||
composites[k] = true;
|
||||
|
||||
return composites;
|
||||
}
|
||||
|
||||
__gshared BitArray composites;
|
||||
|
||||
bool isEmirp(uint n) nothrow @nogc {
|
||||
uint reverse(uint n) pure nothrow @safe @nogc {
|
||||
uint r;
|
||||
for (r = 0; n; n /= 10)
|
||||
r = r * 10 + (n % 10);
|
||||
return r;
|
||||
}
|
||||
|
||||
immutable r = reverse(n);
|
||||
// BitArray doesn't perform bound tests yet.
|
||||
assert(n < composites.length && r < composites.length);
|
||||
return r != n && !composites[n] && !composites[r];
|
||||
}
|
||||
|
||||
void main() {
|
||||
composites = 1_000_000.sieve;
|
||||
|
||||
auto uints = uint.max.iota;
|
||||
writeln("First 20:\n", uints.filter!isEmirp.take(20));
|
||||
writeln("Between 7700 and 8000:\n",
|
||||
iota(7_700, 8_001).filter!isEmirp);
|
||||
writeln("10000th: ", uints.filter!isEmirp.drop(9_999).front);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue