Primality by Wilson's theorem: Difference between revisions

m
syntax highlighting fixup automation
(Added solution for EDSAC.)
m (syntax highlighting fixup automation)
Line 18:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F is_wprime(Int64 n)
R n > 1 & (n == 2 | (n % 2 & (factorial(n - 1) + 1) % n == 0))
 
V c = 20
print(‘Primes under #.:’.format(c), end' "\n ")
print((0 .< c).filter(n -> is_wprime(n)))</langsyntaxhighlight>
 
{{out}}
Line 32:
 
=={{header|8086 Assembly}}==
<langsyntaxhighlight lang="asm"> cpu 8086
org 100h
section .text
Line 75:
section .data
db '*****' ; Space to hold ASCII number for output
numbuf: db 13,10,'$'</langsyntaxhighlight>
 
{{out}}
Line 136:
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">--
<lang Ada>--
-- Determine primality using Wilon's theorem.
-- Uses the approach from Algol W
Line 176:
end Main;
 
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 192:
{{Trans|ALGOL W}}
As with many samples on this page, applies the modulo operation at each step in calculating the factorial, to avoid needing large integeres.
<langsyntaxhighlight lang="algol68">BEGIN
# find primes using Wilson's theorem: #
# p is prime if ( ( p - 1 )! + 1 ) mod p = 0 #
Line 207:
FOR i TO 100 DO IF is wilson prime( i ) THEN print( ( " ", whole( i, 0 ) ) ) FI OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 215:
=={{header|ALGOL W}}==
As with the APL, Tiny BASIC and other samples, this computes the factorials mod p at each multiplication to avoid needing numbers larger than the 32 bit limit.
<langsyntaxhighlight lang="algolw">begin
% find primes using Wilson's theorem: %
% p is prime if ( ( p - 1 )! + 1 ) mod p = 0 %
Line 231:
 
for i := 1 until 100 do if isWilsonPrime( i ) then writeon( i_w := 1, s_w := 0, " ", i );
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 242:
multiplication. This is necessary for the function to work correctly with more than the first few numbers.
 
<langsyntaxhighlight APLlang="apl">wilson ← {⍵<2:0 ⋄ (⍵-1)=(⍵|×)/⍳⍵-1}</langsyntaxhighlight>
 
{{out}}
Line 252:
The naive version (using APL's built-in factorial) looks like this:
 
<langsyntaxhighlight APLlang="apl">naiveWilson ← {⍵<2:0 ⋄ 0=⍵|1+!⍵-1}</langsyntaxhighlight>
 
But due to loss of precision with large floating-point values, it only works correctly up to number 19 even with ⎕CT set to zero:
Line 264:
Nominally, the AppleScript solution would be as follows, the 'mod n' at every stage of the factorial being to keep the numbers within the range the language can handle:
 
<langsyntaxhighlight lang="applescript">on isPrime(n)
if (n < 2) then return false
set f to n - 1
Line 279:
if (isPrime(n)) then set end of output to n
end repeat
output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{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}</langsyntaxhighlight>
 
In fact, though, the modding by n after every multiplication means there are only three possibilities for the final value of f: n - 1 (if n's a prime), 2 (if n's 4), or 0 (if n's any other non-prime). So the test at the end of the handler could be simplified. Another thing is that if f becomes 0 at some point in the repeat, it obviously stays that way for the remaining iterations, so quite a bit of time can be saved by testing for it and returning <tt>false</tt> immediately if it occurs. And if 2 and its multiples are caught before the repeat, any other non-prime will guarantee a jump out of the handler. Simply reaching the end will mean n's a prime.
Line 288:
It turns out too that <tt>false</tt> results only occur when multiplying numbers between √n and n - √n and that only multiplying numbers in this range still leads to the correct outcomes. And if this isn't abusing Wilson's theorem enough, multiples of 2 and 3 can be prechecked and omitted from the "factorial" process altogether, much as they can be skipped in tests for primality by trial division:
 
<langsyntaxhighlight lang="applescript">on isPrime(n)
-- Check for numbers < 2 and 2 & 3 and their multiples.
if (n < 4) then return (n > 1)
Line 301:
return true
end isPrime</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">factorial: function [x]-> product 1..x
 
wprime?: function [n][
Line 313:
 
print "Primes below 20 via Wilson's theorem:"
print select 1..20 => wprime?</langsyntaxhighlight>
 
{{out}}
Line 320:
2 3 5 7 11 13 17 19</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f PRIMALITY_BY_WILSONS_THEOREM.AWK
# converted from FreeBASIC
Line 343:
return(fct == n-1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 358:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">function wilson_prime(n)
fct = 1
for i = 2 to n-1
Line 370:
if wilson_prime(i) then print i; " ";
next i
end</langsyntaxhighlight>
 
==={{header|QBasic}}===
Line 377:
{{works with|Run BASIC}}
{{trans|FreeBASIC}}
<langsyntaxhighlight QBasiclang="qbasic">FUNCTION wilsonprime(n)
fct = 1
FOR i = 2 TO n - 1
Line 388:
FOR i = 2 TO 100
IF wilsonprime(i) THEN PRINT i; " ";
NEXT i</langsyntaxhighlight>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight PureBasiclang="purebasic">Procedure wilson_prime(n.i)
fct.i = 1
For i.i = 2 To n-1
Line 413:
PrintN("")
Input()
CloseConsole()</langsyntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|QBasic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="runbasic">print "Primes below 100"
for i = 2 to 100
if wilsonprime(i) = 1 then print i; " ";
Line 430:
next i
if fct = n-1 then wilsonprime = 1 else wilsonprime = 0
end function</langsyntaxhighlight>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="qbasic">FUNCTION wilsonprime(n)
LET fct = 1
FOR i = 2 TO n - 1
Line 445:
IF wilsonprime(i) = 1 THEN PRINT i; " ";
NEXT i
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">print "Primes below 100\n"
for i = 2 to 100
if wilson_prime(i) print i, " ";
Line 462:
next i
if fct = n-1 then return True else return False : fi
end sub</langsyntaxhighlight>
 
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Line 504:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Is 2 prime: 1
Line 530:
{{libheader|System.Numerics}}
Performance comparison to Sieve of Eratosthenes.
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Collections;
Line 597:
WriteLine(); WriteLine("\nTime taken: {0}ms\n", (DateTime.Now - st).TotalMilliseconds);
}
}</langsyntaxhighlight>
{{out|Output @ Tio.run}}
<pre style="white-space: pre-wrap;">--- Wilson's theorem method ---
Line 638:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 672:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 707:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Wilson primality test
wilson = proc (n: int) returns (bool)
if n<2 then return (false) end
Line 726:
end
stream$putl(po, "")
end start_up </langsyntaxhighlight>
{{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>
Line 732:
=={{header|Common Lisp}}==
 
<syntaxhighlight lang="lisp">
<lang Lisp>
(defun factorial (n)
(if (< n 2) 1 (* n (factorial (1- n)))) )
Line 741:
(unless (zerop n)
(zerop (mod (1+ (factorial (1- n))) n)) ))
</syntaxhighlight>
</lang>
 
{{out}}
Line 759:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# Wilson primality test
Line 786:
i := i + 1;
end loop;
print_nl();</langsyntaxhighlight>
 
{{out}}
Line 794:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.bigint;
import std.stdio;
 
Line 820:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre>Primes less than 100 testing by Wilson's Theorem
Line 828:
{{trans|Pascal}}
A translation of the Pascal short-cut algorithm, for 17-bit) EDSAC signed integers. Finding primes in the range 65436..65536 took 80 EDSAC minutes, so there is not much point in implementing the unshortened algorithm or extending to 35-bit integers.
<langsyntaxhighlight lang="edsac">
[Primes by Wilson's Theoem, for Rosetta Code.]
[EDSAC program, Initial Orders 2.]
Line 962:
PF [acc = 0 on entry]
[end]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 970:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
#! /usr/bin/escript
 
Line 984:
io:format("The first few primes (via Wilson's theorem) are: ~n~p~n",
[[K || K <- lists:seq(1, 128), isprime(K)]]).
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 993:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Wilsons theorem. Nigel Galloway: August 11th., 2020
let wP(n,g)=(n+1I)%g=0I
let fN=Seq.unfold(fun(n,g)->Some((n,g),((n*g),(g+1I))))(1I,2I)|>Seq.filter wP
fN|>Seq.take 120|>Seq.iter(fun(_,n)->printf "%A " n);printfn "\n"
fN|>Seq.skip 999|>Seq.take 15|>Seq.iter(fun(_,n)->printf "%A " n);printfn ""</langsyntaxhighlight>
{{out}}
<pre>
Line 1,008:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: formatting grouping io kernel lists lists.lazy math
math.factorials math.functions prettyprint sequences ;
 
Line 1,024:
"1000th through 1015th primes:" print
16 primes 999 [ cdr ] times ltake list>array
[ pprint bl ] each nl</langsyntaxhighlight>
{{out}}
<pre>
Line 1,058:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Wilson(n) = if ((n-1)!+1)|n = 0 then 1 else 0 fi.;</langsyntaxhighlight>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">
<lang Forth>
: fac-mod ( n m -- r )
>r 1 swap
Line 1,073:
: .primes ( n -- )
cr 2 ?do i ?prime if i . then loop ;
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,081:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function wilson_prime( n as uinteger ) as boolean
dim as uinteger fct=1, i
for i = 2 to n-1
Line 1,093:
for i as uinteger = 2 to 100
if wilson_prime(i) then print i,
next i</langsyntaxhighlight>
{{out}}
Primes below 100
Line 1,111:
 
=={{Header|GAP}}==
<langsyntaxhighlight lang="gap"># find primes using Wilson's theorem:
# p is prime if ( ( p - 1 )! + 1 ) mod p = 0
Line 1,123:
prime := [];
for i in [ -4 .. 100 ] do if isWilsonPrime( i ) then Add( prime, i ); fi; od;
Display( prime );</langsyntaxhighlight>
 
{{out}}
Line 1,135:
 
Presumably we're not allowed to make any trial divisions here except by the number two where all even positive integers, except two itself, are obviously composite.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,209:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,244:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import qualified Data.Text as T
import Data.List
 
Line 1,278:
top = replicate (length hr) hor
bss = map (\ps -> map (flip replicate ' ') $ zipWith (-) ms ps) $ vss
zss@(z:zs) = zipWith (\us bs -> (concat $ zipWith (\x y -> (ver:x) ++ y) us bs) ++ [ver]) contents bss</langsyntaxhighlight>
{{out}}
<pre>
Line 1,314:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
wilson=: 0 = (| !&.:<:)
(#~ wilson) x: 2 + i. 30
2 3 5 7 11 13 17 19 23 29 31
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
Wilson's theorem is an ''extremely'' inefficient way of testing for primality. As a result, optimizations such as caching factorials not performed.
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
 
Line 1,353:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,368:
 
''''Adapted from Julia and Nim''''
<langsyntaxhighlight lang="jq">## Compute (n - 1)! mod m.
def facmod($n; $m):
reduce range(2; $n+1) as $k (1; (. * $k) % $m);
Line 1,379:
# Notice that `infinite` can be used as the second argument of `range`:
"First 10 primes after 7900:",
[limit(10; range(7900; infinite) | select(isPrime))]</langsyntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">
<lang sh>
Prime numbers between 2 and 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]
First 10 primes after 7900:
[7901,7907,7919,7927,7933,7937,7949,7951,7963,7993]</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">iswilsonprime(p) = (p < 2 || (p > 2 && iseven(p))) ? false : foldr((x, y) -> (x * y) % p, 1:p - 1) == p - 1
 
wilsonprimesbetween(n, m) = [i for i in n:m if iswilsonprime(i)]
Line 1,394:
println("First 120 Wilson primes: ", wilsonprimesbetween(1, 1000)[1:120])
println("\nThe first 40 Wilson primes above 7900 are: ", wilsonprimesbetween(7900, 9000)[1:40])
</langsyntaxhighlight>{{out}}
<pre>
First 120 Wilson primes: [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]
Line 1,402:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- primality by Wilson's theorem
function isWilsonPrime( n )
Line 1,417:
io.write( " " .. n )
end
end</langsyntaxhighlight>
{{out}}
<pre>
Line 1,424:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[WilsonPrimeQ]
WilsonPrimeQ[1] = False;
WilsonPrimeQ[p_Integer] := Divisible[(p - 1)! + 1, p]
Select[Range[100], WilsonPrimeQ]</langsyntaxhighlight>
{{out}}
Prime factors up to a 100:
Line 1,433:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, sugar
 
proc facmod(n, m: int): int =
Line 1,448:
 
echo "Prime numbers between 2 and 100:"
echo primes.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 1,455:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">Wilson(n) = prod(i=1,n-1,Mod(i,n))==-1
</syntaxhighlight>
</lang>
 
=={{header|Pascal}}==
Line 1,464:
 
(2) Short cut, based on an observation in the AppleScript solution: if during the calculation of (n - 1)! a partial product is divisible by n, then n is not prime. In fact it suffices for a partial product and n to have a common factor greater than 1. Further, such a common factor must be present in s!, where s = floor(sqrt(n)). Having got s! modulo n we find its HCF with n by Euclid's algorithm; then n is prime if and only if the HCF is 1.
<langsyntaxhighlight lang="pascal">
program PrimesByWilson;
uses SysUtils;
Line 1,538:
ShowPrimes( @WilsonShortCut, 4294967195, 4294967295 {= 2^32 - 1});
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,558:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,579:
 
say $ends_in_3;
say $ends_in_7;</langsyntaxhighlight>
{{out}}
<pre>3 13 23 43 53 73 83 103 113 163 173 193 223 233 263 283 293 313 353 373 383 433 443 463 503
Line 1,586:
=={{header|Phix}}==
Uses the modulus method to avoid needing gmp, which was in fact about 7 times slower (when calculating the full factorials).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">wilson</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">facmod</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
Line 1,609:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" '' builtin: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1015</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">..</span><span style="color: #000000;">1015</span><span style="color: #0000FF;">]})</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,620:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Show some primes (via Wilson's theorem).
Line 1,648:
Find a factorial of the number minus 1. Bump the factorial.
If the factorial is evenly divisible by the number, say yes.
Say no.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,655:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">/* primality by Wilson's theorem */
wilson: procedure options( main );
declare n binary(15)fixed;
Line 1,674:
end;
end;
end wilson ;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,684:
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<langsyntaxhighlight lang="pli">100H: /* FIND PRIMES USING WILSON'S THEOREM: */
/* P IS PRIME IF ( ( P - 1 )! + 1 ) MOD P = 0 */
 
Line 1,732:
END;
 
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 1,747:
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page.
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.
<langsyntaxhighlight lang="pli">/* PRIMALITY BY WILSON'S THEOREM */
wilson_100H: procedure options (main);
 
Line 1,798:
END;
 
EOF: end wilson_100H ;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,806:
=={{header|Python}}==
No attempt is made to optimise this as this method is a [https://en.wikipedia.org/wiki/Wilson%27s_theorem#Primality_tests very poor primality test].
<langsyntaxhighlight lang="python">from math import factorial
 
def is_wprime(n):
Line 1,818:
c = int(input('Enter upper limit: '))
print(f'Primes under {c}:')
print([n for n in range(c) if is_wprime(n)])</langsyntaxhighlight>
 
{{out}}
Line 1,826:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ 1 swap times [ i 1+ * ] ] is ! ( n --> n )
 
[ dup 2 < iff
Line 1,836:
500 times
[ i^ prime if
[ i^ echo sp ] ]</langsyntaxhighlight>
 
{{out}}
Line 1,847:
Not a particularly recommended way to test for primality, especially for larger numbers. It ''works'', but is slow and memory intensive.
 
<syntaxhighlight lang="raku" perl6line>sub postfix:<!> (Int $n) { (constant f = 1, |[\*] 1..*)[$n] }
 
sub is-wilson-prime (Int $p where * > 1) { (($p - 1)! + 1) %% $p }
Line 1,864:
 
put "\n1000th through 1015th primes:";
put $wilsons[999..1014];</langsyntaxhighlight>
{{out}}
<pre> p prime?
Line 1,899:
 
Also, a "pretty print" was used to align the displaying of a list.
<langsyntaxhighlight lang="rexx">/*REXX pgm tests for primality via Wilson's theorem: a # is prime if p divides (p-1)! +1*/
parse arg LO zz /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 120 /*Not specified? Then use the default.*/
Line 1,951:
oo= oo _ /*display a line. */
end /*k*/ /*does pretty print.*/
if oo\='' then say substr(oo, 2); return /*display residual (if any overflowed).*/</langsyntaxhighlight>
Programming note: &nbsp; This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program &nbsp; (or
BIF) &nbsp; which is used to determine the screen width
Line 1,987:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,002:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,026:
 
Alternative version computing the factorials modulo n so as to avoid overflow.
<langsyntaxhighlight lang="ring"># primality by Wilson's theorem
 
limit = 100
Line 2,042:
fmodp %= n
next i
return fmodp = n - 1</langsyntaxhighlight>
 
{{out}}
Line 2,050:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def w_prime?(i)
return false if i < 2
((1..i-1).inject(&:*) + 1) % i == 0
Line 2,056:
 
p (1..100).select{|n| w_prime?(n) }
</syntaxhighlight>
</lang>
{{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]
Line 2,062:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn factorial_mod(mut n: u32, p: u32) -> u32 {
let mut f = 1;
while n != 0 && f != 0 {
Line 2,102:
p += 1;
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,137:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_wilson_prime_slow(n) {
n > 1 || return false
(n-1)! % n == n-1
Line 2,151:
 
say is_wilson_prime_fast(2**43 - 1) #=> false
say is_wilson_prime_fast(2**61 - 1) #=> true</langsyntaxhighlight>
 
=={{header|Swift}}==
Line 2,157:
Using a BigInt library.
 
<langsyntaxhighlight lang="swift">import BigInt
 
func factorial<T: BinaryInteger>(_ n: T) -> T {
Line 2,176:
}
 
print((1...100).map({ BigInt($0) }).filter(isWilsonPrime))</langsyntaxhighlight>
 
{{out}}
Line 2,183:
 
=={{header|Tiny BASIC}}==
<langsyntaxhighlight lang="tinybasic"> PRINT "Number to test"
INPUT N
IF N < 0 THEN LET N = -N
Line 2,208:
40 REM zero and one are nonprimes by definition
PRINT "It is not prime"
END</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 2,214:
{{libheader|Wren-fmt}}
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.
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 2,224:
for (p in 1..19) {
Fmt.print("$2d -> $s", p, wilson.call(p) ? "prime" : "not prime")
}</langsyntaxhighlight>
 
{{out}}
Line 2,251:
=={{header|zkl}}==
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library and primes
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
fcn isWilsonPrime(p){
if(p<=1 or (p%2==0 and p!=2)) return(False);
BI(p-1).factorial().add(1).mod(p) == 0
}
fcn wPrimesW{ [2..].tweak(fcn(n){ isWilsonPrime(n) and n or Void.Skip }) }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">numbers:=T(2, 3, 9, 15, 29, 37, 47, 57, 67, 77, 87, 97, 237, 409, 659);
println(" n prime");
println("--- -----");
Line 2,267:
 
println("\nThe 1,000th to 1,015th prime numbers are:");
wPrimesW().drop(999).walk(15).concat(" ").println();</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits