Mersenne primes: Difference between revisions
Content added Content deleted
m (→{{header|Kotlin}}: Replaced 'shiftLeft' function with more idiomatic 'shl' infix function introduced in v1.2.) |
(Added D) |
||
Line 44: | Line 44: | ||
end repeat |
end repeat |
||
</lang> |
</lang> |
||
=={{header|D}}== |
|||
Simplest thing that could possibly work. Using better primality tests will allow for more results to be calculated in a reasonable amount of time. |
|||
<lang D>import std.bigint; |
|||
import std.stdio; |
|||
bool isPrime(BigInt bi) { |
|||
if (bi < 2) return false; |
|||
if (bi % 2 == 0) return bi == 2; |
|||
if (bi % 3 == 0) return bi == 3; |
|||
auto test = BigInt(5); |
|||
while (test * test < bi) { |
|||
if (bi % test == 0) return false; |
|||
test += 2; |
|||
if (bi % test == 0) return false; |
|||
test += 4; |
|||
} |
|||
return true; |
|||
} |
|||
void main() { |
|||
auto base = BigInt(2); |
|||
for (int pow=1; pow<32; pow++) { |
|||
if (isPrime(base-1)) { |
|||
writeln("2 ^ ", pow, " - 1"); |
|||
} |
|||
base *= 2; |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>2 ^ 2 - 1 |
|||
2 ^ 3 - 1 |
|||
2 ^ 5 - 1 |
|||
2 ^ 7 - 1 |
|||
2 ^ 13 - 1 |
|||
2 ^ 17 - 1 |
|||
2 ^ 19 - 1 |
|||
2 ^ 31 - 1</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |