Wilson primes of order n: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 25: Line 25:
Algol 68G supports long integers, however the precision must be specified.<br>
Algol 68G supports long integers, however the precision must be specified.<br>
As the memory required for a limit of 11 000 would exceed he maximum supported by Algol 68G under WIndows, a limit of 5 500 is used here, which is sufficient to find all but the 4th order Wilson prime.
As the memory required for a limit of 11 000 would exceed he maximum supported by Algol 68G under WIndows, a limit of 5 500 is used here, which is sufficient to find all but the 4th order Wilson prime.
<lang algol68>BEGIN # find Wilson primes of order n, primes such that: #
<syntaxhighlight lang="algol68">BEGIN # find Wilson primes of order n, primes such that: #
# ( ( n - 1 )! x ( p - n )! - (-1)^n ) mod p^2 = 0 #
# ( ( n - 1 )! x ( p - n )! - (-1)^n ) mod p^2 = 0 #
INT limit = 5 508; # max prime to consider #
INT limit = 5 508; # max prime to consider #
Line 71: Line 71:
print( ( newline ) )
print( ( newline ) )
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 93: Line 93:
==={{header|BASIC256}}===
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang BASIC256>function isPrime(v)
<syntaxhighlight lang="basic256">function isPrime(v)
if v <= 1 then return False
if v <= 1 then return False
for i = 2 To int(sqr(v))
for i = 2 To int(sqr(v))
Line 124: Line 124:
print
print
next n
next n
end</lang>
end</syntaxhighlight>


==={{header|QBasic}}===
==={{header|QBasic}}===
Line 130: Line 130:
{{works with|QuickBasic}}
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang QBasic>FUNCTION isPrime (ValorEval)
<syntaxhighlight lang="qbasic">FUNCTION isPrime (ValorEval)
IF ValorEval < 2 THEN isPrime = False
IF ValorEval < 2 THEN isPrime = False
IF ValorEval MOD 2 = 0 THEN isPrime = 2
IF ValorEval MOD 2 = 0 THEN isPrime = 2
Line 164: Line 164:
PRINT
PRINT
NEXT n
NEXT n
END</lang>
END</syntaxhighlight>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang yabasic>print "n: Wilson primes"
<syntaxhighlight lang="yabasic">print "n: Wilson primes"
print "---------------------"
print "---------------------"
for n = 1 to 11
for n = 1 to 11
Line 202: Line 202:
prod = mod((p2 + prod - (-1)**n), p2)
prod = mod((p2 + prod - (-1)**n), p2)
if prod = 0 then return True else return False : fi
if prod = 0 then return True else return False : fi
end sub</lang>
end sub</syntaxhighlight>




=={{header|C++}}==
=={{header|C++}}==
{{libheader|GMP}}
{{libheader|GMP}}
<lang cpp>#include <iomanip>
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
Line 251: Line 251:
std::cout << '\n';
std::cout << '\n';
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 272: Line 272:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Wilson primes. Nigel Galloway: July 31st., 2021
// Wilson primes. Nigel Galloway: July 31st., 2021
let rec fN g=function n when n<2I->g |n->fN(n*g)(n-1I)
let rec fN g=function n when n<2I->g |n->fN(n*g)(n-1I)
let fG (n:int)(p:int)=let g,p=bigint n,bigint p in (((fN 1I (g-1I))*(fN 1I (p-g))-(-1I)**n)%(p*p))=0I
let fG (n:int)(p:int)=let g,p=bigint n,bigint p in (((fN 1I (g-1I))*(fN 1I (p-g))-(-1I)**n)%(p*p))=0I
[1..11]|>List.iter(fun n->printf "%2d -> " n; let fG=fG n in pCache|>Seq.skipWhile((>)n)|>Seq.takeWhile((>)11000)|>Seq.filter fG|>Seq.iter(printf "%d "); printfn "")
[1..11]|>List.iter(fun n->printf "%2d -> " n; let fG=fG n in pCache|>Seq.skipWhile((>)n)|>Seq.takeWhile((>)11000)|>Seq.filter fG|>Seq.iter(printf "%d "); printfn "")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 295: Line 295:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting infix io kernel literals math math.functions
<syntaxhighlight lang="factor">USING: formatting infix io kernel literals math math.functions
math.primes math.ranges prettyprint sequences sequences.extras ;
math.primes math.ranges prettyprint sequences sequences.extras ;


Line 320: Line 320:


" n: Wilson primes\n--------------------" print
" n: Wilson primes\n--------------------" print
11 [1,b] [ order. ] each</lang>
11 [1,b] [ order. ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 340: Line 340:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
This excludes the trivial case p=n=2.
This excludes the trivial case p=n=2.
<lang freebasic>#include "isprime.bas"
<syntaxhighlight lang="freebasic">#include "isprime.bas"


function is_wilson( n as uinteger, p as uinteger ) as boolean
function is_wilson( n as uinteger, p as uinteger ) as boolean
Line 367: Line 367:
print
print
next n
next n
</syntaxhighlight>
</lang>
{{out}}<pre>
{{out}}<pre>
n: Wilson primes
n: Wilson primes
Line 387: Line 387:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 426: Line 426:
fmt.Println()
fmt.Println()
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 446: Line 446:


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang gwbasic>10 PRINT "n: Wilson primes"
<syntaxhighlight lang="gwbasic">10 PRINT "n: Wilson primes"
20 PRINT "--------------------"
20 PRINT "--------------------"
30 FOR N = 1 TO 11
30 FOR N = 1 TO 11
Line 487: Line 487:
3000 REM PROD# MOD P2 fails if PROD#>32767 so brew our own modulus function
3000 REM PROD# MOD P2 fails if PROD#>32767 so brew our own modulus function
3010 PROD# = PROD# - INT(PROD#/P2)*P2
3010 PROD# = PROD# - INT(PROD#/P2)*P2
3020 RETURN</lang>
3020 RETURN</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.math.BigInteger;
<syntaxhighlight lang="java">import java.math.BigInteger;
import java.util.*;
import java.util.*;


Line 538: Line 538:
return primes;
return primes;
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 568: Line 568:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;
<syntaxhighlight lang="jq">def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;


# For 0 <= $n <= ., factorials[$n] is $n !
# For 0 <= $n <= ., factorials[$n] is $n !
Line 577: Line 577:
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;


def primes: 2, (range(3; infinite; 2) | select(is_prime));</lang>
def primes: 2, (range(3; infinite; 2) | select(is_prime));</syntaxhighlight>
'''Wilson primes'''
'''Wilson primes'''
<lang jq># Input: the limit of $p
<syntaxhighlight lang="jq"># Input: the limit of $p
def wilson_primes:
def wilson_primes:
def sgn: if . % 2 == 0 then 1 else -1 end;
def sgn: if . % 2 == 0 then 1 else -1 end;
Line 594: Line 594:
% ($p*$p) == 0 )) ])" );
% ($p*$p) == 0 )) ])" );


11000 | wilson_primes</lang>
11000 | wilson_primes</syntaxhighlight>
{{out}}
{{out}}
gojq -ncr -f rc-wilson-primes.jq
gojq -ncr -f rc-wilson-primes.jq
Line 615: Line 615:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Wren}}
{{trans|Wren}}
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function wilsonprimes(limit = 11000)
function wilsonprimes(limit = 11000)
Line 633: Line 633:


wilsonprimes()
wilsonprimes()
</syntaxhighlight>
</lang>
Output: Same as Wren example.
Output: Same as Wren example.


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[WilsonPrime]
<syntaxhighlight lang="mathematica">ClearAll[WilsonPrime]
WilsonPrime[n_Integer] := Module[{primes, out},
WilsonPrime[n_Integer] := Module[{primes, out},
primes = Prime[Range[PrimePi[11000]]];
primes = Prime[Range[PrimePi[11000]]];
Line 651: Line 651:
,
,
{n, 1, 11}
{n, 1, 11}
]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>{5,13,563}
<pre>{5,13,563}
Line 669: Line 669:
{{libheader|bignum}}
{{libheader|bignum}}
As in Nim there is not (not yet?) a standard module to deal with big numbers, we use the third party module “bignum”.
As in Nim there is not (not yet?) a standard module to deal with big numbers, we use the third party module “bignum”.
<lang Nim>import strformat, strutils
<syntaxhighlight lang="nim">import strformat, strutils
import bignum
import bignum


Line 698: Line 698:
if f mod (p * p) == 0:
if f mod (p * p) == 0:
wilson.add p
wilson.add p
echo &"{n:2}: ", wilson.join(" ")</lang>
echo &"{n:2}: ", wilson.join(" ")</syntaxhighlight>


{{out}}
{{out}}
Line 717: Line 717:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use ntheory <primes factorial>;
use ntheory <primes factorial>;
Line 725: Line 725:
for my $n (1..11) {
for my $n (1..11) {
printf "%3d: %s\n", $n, join ' ', grep { $_ >= $n && 0 == (factorial($n-1) * factorial($_-$n) - (-1)**$n) % $_**2 } @primes
printf "%3d: %s\n", $n, join ' ', grep { $_ >= $n && 0 == (factorial($n-1) * factorial($_-$n) - (-1)**$n) % $_**2 } @primes
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 1: 5 13 563
<pre> 1: 5 13 563
Line 741: Line 741:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Wren}}
{{trans|Wren}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">11000</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">11000</span>
Line 767: Line 767:
<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;">"\n"</span><span style="color: #0000FF;">)</span>
<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;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
Output: Same as Wren example.
Output: Same as Wren example.


=={{header|Prolog}}==
=={{header|Prolog}}==
{{works with|SWI Prolog}}
{{works with|SWI Prolog}}
<lang prolog>main:-
<syntaxhighlight lang="prolog">main:-
wilson_primes(11000).
wilson_primes(11000).


Line 813: Line 813:
M1 is M + 1,
M1 is M + 1,
F1 is F * M1,
F1 is F * M1,
make_factorials(N, M1, F1).</lang>
make_factorials(N, M1, F1).</syntaxhighlight>


Module for finding prime numbers up to some limit:
Module for finding prime numbers up to some limit:
<lang prolog>:- module(prime_numbers, [find_prime_numbers/1, is_prime/1]).
<syntaxhighlight lang="prolog">:- module(prime_numbers, [find_prime_numbers/1, is_prime/1]).
:- dynamic is_prime/1.
:- dynamic is_prime/1.


Line 857: Line 857:
cross_out(S, N, P):-
cross_out(S, N, P):-
Q is S + 2 * P,
Q is S + 2 * P,
cross_out(Q, N, P).</lang>
cross_out(Q, N, P).</syntaxhighlight>


{{out}}
{{out}}
Line 878: Line 878:
=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(require math/number-theory)
(require math/number-theory)
Line 893: Line 893:


(for ((n (in-range 1 (add1 11))))
(for ((n (in-range 1 (add1 11))))
(printf "~a: ~a~%" n (filter (wilson-prime? n) primes<11000)))</lang>
(printf "~a: ~a~%" n (filter (wilson-prime? n) primes<11000)))</syntaxhighlight>


{{out}}
{{out}}
Line 910: Line 910:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6># Factorial
<syntaxhighlight lang="raku" line># Factorial
sub postfix:<!> (Int $n) { (constant f = 1, |[\×] 1..*)[$n] }
sub postfix:<!> (Int $n) { (constant f = 1, |[\×] 1..*)[$n] }


Line 927: Line 927:
.say for (1..40).hyper(:1batch).map: -> \𝒏 {
.say for (1..40).hyper(:1batch).map: -> \𝒏 {
sprintf "%3d: %s", 𝒏, @primes.grep( -> \𝒑 { (𝒑 ≥ 𝒏) && ((𝒏 - 1)!⁢(𝒑 - 𝒏)! - (-1) ** 𝒏) %% 𝒑² } ).Str
sprintf "%3d: %s", 𝒏, @primes.grep( -> \𝒑 { (𝒑 ≥ 𝒏) && ((𝒏 - 1)!⁢(𝒑 - 𝒏)! - (-1) ** 𝒏) %% 𝒑² } ).Str
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> n: Wilson primes
<pre> n: Wilson primes
Line 974: Line 974:
=={{header|REXX}}==
=={{header|REXX}}==
For more (extended) results, &nbsp; see this task's discussion page.
For more (extended) results, &nbsp; see this task's discussion page.
<lang rexx>/*REXX program finds and displays Wilson primes: a prime P such that P**2 divides:*/
<syntaxhighlight lang="rexx">/*REXX program finds and displays Wilson primes: a prime P such that P**2 divides:*/
/*────────────────── (n-1)! * (P-n)! - (-1)**n where n is 1 ──◄ 11, and P < 18.*/
/*────────────────── (n-1)! * (P-n)! - (-1)**n where n is 1 ──◄ 11, and P < 18.*/
parse arg oLO oHI hip . /*obtain optional argument from the CL.*/
parse arg oLO oHI hip . /*obtain optional argument from the CL.*/
Line 1,020: Line 1,020:
end /*k*/ /* [↑] only process numbers ≤ √ J */
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,040: Line 1,040:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>require "prime"
<syntaxhighlight lang="ruby">require "prime"
module Modulo
module Modulo
Line 1,058: Line 1,058:
puts "#{n.to_s.rjust(2)}: #{res.inspect}"
puts "#{n.to_s.rjust(2)}: #{res.inspect}"
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre> 1: [5, 13, 563]
<pre> 1: [5, 13, 563]
Line 1,074: Line 1,074:
</pre>
</pre>
=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// rug = "1.13.0"
// rug = "1.13.0"


Line 1,137: Line 1,137:
s = -s;
s = -s;
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,157: Line 1,157:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func is_wilson_prime(p, n = 1) {
<syntaxhighlight lang="ruby">func is_wilson_prime(p, n = 1) {
var m = p*p
var m = p*p
(factorialmod(n-1, m) * factorialmod(p-n, m) - (-1)**n) % m == 0
(factorialmod(n-1, m) * factorialmod(p-n, m) - (-1)**n) % m == 0
Line 1,168: Line 1,168:
for n in (1..11) {
for n in (1..11) {
printf("%3d: %s\n", n, primes.grep {|p| is_wilson_prime(p, n) })
printf("%3d: %s\n", n, primes.grep {|p| is_wilson_prime(p, n) })
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,190: Line 1,190:
{{libheader|Wren-big}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/big" for BigInt
import "/big" for BigInt
import "/fmt" for Fmt
import "/fmt" for Fmt
Line 1,211: Line 1,211:
}
}
System.print()
System.print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}