Primality by Wilson's theorem: Difference between revisions
Add Refal
Not a robot (talk | contribs) (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-
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./gmp" for Mpz
var t = Mpz.new()
var wilson = Fn.new { |p|
if (p < 2) return false
return (
}
var primes = [2]
var i = 3
while (primes.count < 1015) {
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>
------------
2 | true
3 | true
9 | false
15 | false
37 | true
47 | true
57 | false
67 | true
77 | false
87 | false
97 | true
237 | false
409 | true
659 | true
The first 120 prime numbers by Wilson's theorem are:
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>
|