Primality by Wilson's theorem: Difference between revisions
Content deleted Content added
→{{header|Wren}}: Rewritten to use GMP. |
Add Dart implementation |
||
Line 1,021: | Line 1,021: | ||
<pre>Primes less than 100 testing by Wilson's Theorem |
<pre>Primes less than 100 testing by Wilson's Theorem |
||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</pre> |
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</pre> |
||
=={{header|Dart}}== |
|||
{{trans|Swift}} |
|||
<syntaxhighlight lang="Dart"> |
|||
BigInt factorial(BigInt n) { |
|||
if (n == BigInt.zero) { |
|||
return BigInt.one; |
|||
} |
|||
BigInt result = BigInt.one; |
|||
for (BigInt i = n; i > BigInt.zero; i = i - BigInt.one) { |
|||
result *= i; |
|||
} |
|||
return result; |
|||
} |
|||
bool isWilsonPrime(BigInt n) { |
|||
if (n < BigInt.from(2)) { |
|||
return false; |
|||
} |
|||
return (factorial(n - BigInt.one) + BigInt.one) % n == BigInt.zero; |
|||
} |
|||
void main() { |
|||
var wilsonPrimes = []; |
|||
for (var i = BigInt.one; i <= BigInt.from(100); i += BigInt.one) { |
|||
if (isWilsonPrime(i)) { |
|||
wilsonPrimes.add(i); |
|||
} |
|||
} |
|||
print(wilsonPrimes); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] |
|||
</pre> |
|||
=={{header|Draco}}== |
=={{header|Draco}}== |