Wilson primes of order n: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 25:
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.
<langsyntaxhighlight lang="algol68">BEGIN # find Wilson primes of order n, primes such that: #
# ( ( n - 1 )! x ( p - n )! - (-1)^n ) mod p^2 = 0 #
INT limit = 5 508; # max prime to consider #
Line 71:
print( ( newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 93:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">function isPrime(v)
if v <= 1 then return False
for i = 2 To int(sqr(v))
Line 124:
print
next n
end</langsyntaxhighlight>
 
==={{header|QBasic}}===
Line 130:
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight QBasiclang="qbasic">FUNCTION isPrime (ValorEval)
IF ValorEval < 2 THEN isPrime = False
IF ValorEval MOD 2 = 0 THEN isPrime = 2
Line 164:
PRINT
NEXT n
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">print "n: Wilson primes"
print "---------------------"
for n = 1 to 11
Line 202:
prod = mod((p2 + prod - (-1)**n), p2)
if prod = 0 then return True else return False : fi
end sub</langsyntaxhighlight>
 
 
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 251:
std::cout << '\n';
}
}</langsyntaxhighlight>
 
{{out}}
Line 272:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Wilson primes. Nigel Galloway: July 31st., 2021
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
[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}}
<pre>
Line 295:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting infix io kernel literals math math.functions
math.primes math.ranges prettyprint sequences sequences.extras ;
 
Line 320:
 
" n: Wilson primes\n--------------------" print
11 [1,b] [ order. ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 340:
=={{header|FreeBASIC}}==
This excludes the trivial case p=n=2.
<langsyntaxhighlight lang="freebasic">#include "isprime.bas"
 
function is_wilson( n as uinteger, p as uinteger ) as boolean
Line 367:
print
next n
</syntaxhighlight>
</lang>
{{out}}<pre>
n: Wilson primes
Line 387:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 426:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 446:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 PRINT "n: Wilson primes"
20 PRINT "--------------------"
30 FOR N = 1 TO 11
Line 487:
3000 REM PROD# MOD P2 fails if PROD#>32767 so brew our own modulus function
3010 PROD# = PROD# - INT(PROD#/P2)*P2
3020 RETURN</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.math.BigInteger;
import java.util.*;
 
Line 538:
return primes;
}
}</langsyntaxhighlight>
 
{{out}}
Line 568:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;
 
# For 0 <= $n <= ., factorials[$n] is $n !
Line 577:
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def primes: 2, (range(3; infinite; 2) | select(is_prime));</langsyntaxhighlight>
'''Wilson primes'''
<langsyntaxhighlight lang="jq"># Input: the limit of $p
def wilson_primes:
def sgn: if . % 2 == 0 then 1 else -1 end;
Line 594:
% ($p*$p) == 0 )) ])" );
 
11000 | wilson_primes</langsyntaxhighlight>
{{out}}
gojq -ncr -f rc-wilson-primes.jq
Line 615:
=={{header|Julia}}==
{{trans|Wren}}
<langsyntaxhighlight lang="julia">using Primes
 
function wilsonprimes(limit = 11000)
Line 633:
 
wilsonprimes()
</syntaxhighlight>
</lang>
Output: Same as Wren example.
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[WilsonPrime]
WilsonPrime[n_Integer] := Module[{primes, out},
primes = Prime[Range[PrimePi[11000]]];
Line 651:
,
{n, 1, 11}
]</langsyntaxhighlight>
{{out}}
<pre>{5,13,563}
Line 669:
{{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”.
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
import bignum
 
Line 698:
if f mod (p * p) == 0:
wilson.add p
echo &"{n:2}: ", wilson.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 717:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory <primes factorial>;
Line 725:
for my $n (1..11) {
printf "%3d: %s\n", $n, join ' ', grep { $_ >= $n && 0 == (factorial($n-1) * factorial($_-$n) - (-1)**$n) % $_**2 } @primes
}</langsyntaxhighlight>
{{out}}
<pre> 1: 5 13 563
Line 741:
=={{header|Phix}}==
{{trans|Wren}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
Output: Same as Wren example.
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">main:-
wilson_primes(11000).
 
Line 813:
M1 is M + 1,
F1 is F * M1,
make_factorials(N, M1, F1).</langsyntaxhighlight>
 
Module for finding prime numbers up to some limit:
<langsyntaxhighlight lang="prolog">:- module(prime_numbers, [find_prime_numbers/1, is_prime/1]).
:- dynamic is_prime/1.
 
Line 857:
cross_out(S, N, P):-
Q is S + 2 * P,
cross_out(Q, N, P).</langsyntaxhighlight>
 
{{out}}
Line 878:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 893:
 
(for ((n (in-range 1 (add1 11))))
(printf "~a: ~a~%" n (filter (wilson-prime? n) primes<11000)))</langsyntaxhighlight>
 
{{out}}
Line 910:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line># Factorial
sub postfix:<!> (Int $n) { (constant f = 1, |[\×] 1..*)[$n] }
 
Line 927:
.say for (1..40).hyper(:1batch).map: -> \𝒏 {
sprintf "%3d: %s", 𝒏, @primes.grep( -> \𝒑 { (𝒑 ≥ 𝒏) && ((𝒏 - 1)!⁢(𝒑 - 𝒏)! - (-1) ** 𝒏) %% 𝒑² } ).Str
}</langsyntaxhighlight>
{{out}}
<pre> n: Wilson primes
Line 974:
=={{header|REXX}}==
For more (extended) results, &nbsp; see this task's discussion page.
<langsyntaxhighlight 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.*/
parse arg oLO oHI hip . /*obtain optional argument from the CL.*/
Line 1,020:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,040:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
module Modulo
Line 1,058:
puts "#{n.to_s.rjust(2)}: #{res.inspect}"
end
</syntaxhighlight>
</lang>
{{out}}
<pre> 1: [5, 13, 563]
Line 1,074:
</pre>
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// rug = "1.13.0"
 
Line 1,137:
s = -s;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,157:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_wilson_prime(p, n = 1) {
var m = p*p
(factorialmod(n-1, m) * factorialmod(p-n, m) - (-1)**n) % m == 0
Line 1,168:
for n in (1..11) {
printf("%3d: %s\n", n, primes.grep {|p| is_wilson_prime(p, n) })
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,190:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/big" for BigInt
import "/fmt" for Fmt
Line 1,211:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
10,327

edits