Primality by Wilson's theorem: Difference between revisions

Add Refal
(→‎{{header|Wren}}: Rewritten to use GMP.)
(Add Refal)
 
(6 intermediate revisions by 3 users not shown)
Line 1,021:
<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>
 
 
=={{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}}==
Line 1,046 ⟶ 1,089:
{{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|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
func wilson_prime n .
fct = 1
for i = 2 to n - 1
fct = fct * i mod n
.
return if fct = n - 1
.
for i = 2 to 100
if wilson_prime i = 1
write i & " "
.
.
</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|EDSAC order code}}==
{{trans|Pascal}}
Line 1,730 ⟶ 1,795:
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|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
 
INTERNAL FUNCTION(N)
ENTRY TO WILSON.
WHENEVER N.L.2, FUNCTION RETURN 0B
F = 1
THROUGH FM, FOR I = N-1, -1, I.L.2
F = F*I
FM F = F-F/N*N
FUNCTION RETURN N.E.F+1
END OF FUNCTION
 
PRINT COMMENT $ PRIMES UP TO 100$
THROUGH TEST, FOR V=1, 1, V.G.100
TEST WHENEVER WILSON.(V), PRINT FORMAT NUMF, V
 
VECTOR VALUES NUMF = $I3*$
END OF PROGRAM</syntaxhighlight>
{{out}}
<pre>PRIMES UP TO 100
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|Mathematica}}/{{header|Wolfram Language}}==
Line 1,751 ⟶ 1,863:
{{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|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE WilsonPrimes;
FROM InOut IMPORT WriteCard, WriteLn;
 
VAR i: CARDINAL;
 
PROCEDURE Wilson(n: CARDINAL): BOOLEAN;
VAR
f, i: CARDINAL;
BEGIN
IF n<2 THEN RETURN FALSE END;
f := 1;
FOR i := n-1 TO 2 BY -1 DO
f := f*i MOD n
END;
RETURN f + 1 = n
END Wilson;
 
BEGIN
FOR i := 1 TO 100 DO
IF Wilson(i) THEN
WriteCard(i, 3)
END
END;
WriteLn
END 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|Nim}}==
Line 2,346 ⟶ 2,487:
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Filter Wilson <Iota 100>>>;
};
 
Wilson {
s.N, <Compare s.N 2>: '-' = F;
s.N = <Wilson s.N 1 <- s.N 1>>;
s.N s.A 1, <- s.N 1>: { s.A = T; s.X = F; };
s.N s.A s.C = <Wilson s.N <Mod <* s.A s.C> s.N> <- s.C 1>>;
};
 
Iota {
s.N = <Iota 1 s.N>;
s.N s.N = s.N;
s.N s.M = s.N <Iota <+ 1 s.N> s.M>;
};
 
Filter {
s.F = ;
s.F t.I e.X, <Mu s.F t.I>: {
T = t.I <Filter s.F e.X>;
F = <Filter s.F e.X>;
};
};</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|Ring}}==
<syntaxhighlight lang="ring">
Line 2,506 ⟶ 2,674:
7919 7927 7933 7937 7949 7951 7963 7993 8009 8011 8017 8039 8053 8059 8069 8081
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
import scala.math.BigInt
 
object PrimalityByWilsonsTheorem extends App {
println("Primes less than 100 testing by Wilson's Theorem")
(0 to 100).foreach(i => if (isPrime(i)) print(s"$i "))
 
private def isPrime(p: Long): Boolean = {
if (p <= 1) return false
(fact(p - 1).+(BigInt(1))).mod(BigInt(p)) == BigInt(0)
}
 
private def fact(n: Long): BigInt = {
(2 to n.toInt).foldLeft(BigInt(1))((fact, i) => fact * BigInt(i))
}
}
</syntaxhighlight>
{{out}}
<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>
 
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program wilsons_theorem;
print({n : n in {1..100} | wilson n});
 
op wilson(p);
return p>1 and */{1..p-1} mod p = p-1;
end op;
end program;</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|Sidef}}==
2,114

edits