Primality by Wilson's theorem: Difference between revisions

Add Refal
(Add Refal)
 
(11 intermediate revisions by 7 users not shown)
Line 441:
210 if fct = n-1 then print i;
220 end function</syntaxhighlight>
 
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">for i = 2 to 100
 
let f = 1
 
for j = 2 to i - 1
 
let f = (f * j) % i
wait
 
next j
 
if f = i - 1 then
 
print i
 
endif
 
next i
 
end</syntaxhighlight>
{{out| Output}}<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|GW-BASIC}}===
Line 998 ⟶ 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,023 ⟶ 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,355 ⟶ 1,443:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Primality_by_Wilson%27s_theorem}}
 
'''Solution'''
 
[[File:Fōrmulæ - Primality by Wilson's theorem 01.png]]
 
'''Test cases'''
 
[[File:Fōrmulæ - Primality by Wilson's theorem 02.png]]
 
[[File:Fōrmulæ - Primality by Wilson's theorem 03.png]]
 
=={{Header|GAP}}==
Line 1,697 ⟶ 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,718 ⟶ 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,313 ⟶ 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,375 ⟶ 2,576:
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|RPL}}==
{{works with|HP|48S}}
≪ '''IF''' DUP 1 > '''THEN'''
DUP → p j
≪ 1
'''WHILE''' 'j' DECR 1 > '''REPEAT''' j * p MOD '''END'''
1 + p MOD NOT
'''ELSE''' DROP 0 '''END'''
≫ '<span style="color:blue">WILSON?<span>' STO
 
=={{header|Ruby}}==
Line 2,462 ⟶ 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}}==
Line 2,538 ⟶ 2,787:
 
=={{header|Wren}}==
{{libheader|Wren-mathgmp}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./gmp" for Mpz
Due to a limitation in the size of integers which Wren can handle (2^53-1) and lack of big integer support, we can only reliably demonstrate primality using Wilson's theorem for numbers up to 19.
<syntaxhighlight lang="ecmascript">import "./mathfmt" for IntFmt
 
import "/fmt" for Fmt
var t = Mpz.new()
 
var wilson = Fn.new { |p|
if (p < 2) return false
return (Intt.factorial(p-1) + 1) % p == 0
}
 
var primes = [2]
for (p in 1..19) {
var i = 3
Fmt.print("$2d -> $s", p, wilson.call(p) ? "prime" : "not prime")
while (primes.count < 1015) {
}</syntaxhighlight>
if (wilson.call(i)) primes.add(i)
i = i + 2
}
 
var candidates = [2, 3, 9, 15, 29, 37, 47, 57, 67, 77, 87, 97, 237, 409, 659]
System.print(" n | prime?\n------------")
for (cand in candidates) Fmt.print("$3d | $s", cand, wilson.call(cand))
 
System.print("\nThe first 120 prime numbers by Wilson's theorem are:")
Fmt.tprint("$3d", primes[0..119], 20)
 
System.print("\nThe 1,000th to 1,015th prime numbers are:")
System.print(primes[-16..-1].join(" "))</syntaxhighlight>
 
{{out}}
<pre>
1 -> notn | prime?
------------
2 -> prime
2 | true
3 -> prime
3 | true
4 -> not prime
9 | false
5 -> prime
15 | false
6 -> not prime
729 ->| primetrue
37 | true
8 -> not prime
47 | true
9 -> not prime
57 | false
10 -> not prime
67 | true
11 -> prime
77 | false
12 -> not prime
87 | false
13 -> prime
97 | true
14 -> not prime
237 | false
15 -> not prime
409 | true
16 -> not prime
659 | true
17 -> prime
 
18 -> not prime
The first 120 prime numbers by Wilson's theorem are:
19 -> prime
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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541
547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
 
The 1,000th to 1,015th prime numbers are:
7919 7927 7933 7937 7949 7951 7963 7993 8009 8011 8017 8039 8053 8059 8069 8081
</pre>
 
2,096

edits