Primality by trial division: Difference between revisions

Content added Content deleted
No edit summary
Line 1,474: Line 1,474:
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.math;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.math;


bool isPrime1(in int n) pure nothrow {
bool isPrime1(T)(in T n) pure nothrow {
if (n == 2)
// Simple Version
if (n == 2)
return true;
return true;
if (n <= 1 || (n & 1) == 0)
if (n <= 1 || (n & 1) == 0)
return false;
return false;

for(int i = 3; i <= real(n).sqrt; i += 2)
for(T i = 3; i <= real(n).sqrt; i += 2)
if (n % i == 0)
if (n % i == 0)
return false;
return false;
return true;
return true;
}
}



void main() {
void main() {
Line 1,491: Line 1,495:
{{out}}
{{out}}
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

===Version with excluded multiplies of 2 and 3===
===Version with excluded multiplies of 2 and 3===
Same output.
Same output.