Wilson primes of order n: Difference between revisions

m
→‎{{header|RPL}}: removed a debug instruction
(Added PureBasic, Run BASIC and True BASIC)
m (→‎{{header|RPL}}: removed a debug instruction)
 
(7 intermediate revisions by 5 users not shown)
Line 207:
510 prod = prod-int(prod/p2)*p2
520 return</syntaxhighlight>
 
==={{header|MSX Basic}}===
Both the [[#GW-BASIC|GW-BASIC]] and [[#Chipmunk_Basic|Chipmunk Basic]] solutions work without change.
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">XIncludeFile "isprime.pb"
 
Procedure.b isWilson(n.i, p.i)
Define prod.f, i.i, p2.f
If p < n : ProcedureReturn #False
EndIf
prod = 1
p2 = p*p
For i = 1 To n-1
prod = Mod((prod*i), p2)
Next i
For i = 1 To p-n
prod = Mod((prod*i), p2)
Next i
prod = Mod((p2 + prod - Pow(-1,n)), p2)
If prod = 0 :
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
 
OpenConsole()
PrintN(" n: Wilson primes")
PrintN("----------------------")
For n.i = 1 To 11
Print(Str(n) + ": ")
For p.i = 3 To 10099 Step 2
If isPrime(p) And isWilson(n, p) :
Print(Str(p) + " ")
EndIf
Next p
PrintN("")
Next n
 
Input()
CloseConsole()</syntaxhighlight>
 
==={{header|QBasic}}===
Line 292 ⟶ 248:
END</syntaxhighlight>
 
==={{header|RunMSX BASICBasic}}===
Both the [[#GW-BASIC|GW-BASIC]] and [[#Chipmunk_Basic|Chipmunk Basic]] solutions work without change.
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">function isPrime(n)
if n < 2 then isPrime = 0 : goto [exit]
if n = 2 then isPrime = 1 : goto [exit]
if n mod 2 = 0 then isPrime = 0 : goto [exit]
isPrime = 1
for i = 3 to int(n^.5) step 2
if n mod i = 0 then isPrime = 0 : goto [exit]
next i
[exit]
end function
 
function isWilson(n, p)
if p < n then isWilson = 0 : goto [exit]
prod = 1
p2 = p^2
for i = 1 to n-1
prod = (prod*i) mod p2
next i
for i = 1 to p-n
prod = (prod*i) mod p2
next i
prod = (p2 + prod - (-1)^n) mod p2
if prod = 0 then isWilson = 1 : goto [exit]
isWilson = 0
[exit]
end function
 
print " n: Wilson primes"
print "------------------"
for n = 2 to 11
print n;" : ";
for p = 3 to 10499 step 2
if isPrime(p) and isWilson(n, p) then print p; " ";
next p
print
next n
end</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">FUNCTION isPrime(v)
LET FALSE = 0
LET TRUE = 1
 
IF v <= 1 THEN LET isPrime = FALSE
IF v < 4 THEN LET isPrime = TRUE
IF REMAINDER(v, 2) = 0 THEN LET isPrime = FALSE
IF v < 9 THEN LET isPrime = TRUE
IF REMAINDER(v, 3) = 0 THEN
LET isPrime = FALSE
ELSE
LET r = INT(SQR(v))
LET f = 5
DO WHILE f <= r
IF REMAINDER(v, f) = 0 OR REMAINDER(v, (f+2)) = 0 THEN LET isPrime = FALSE
LET f = f + 6
LOOP
END IF
LET isPrime = TRUE
END FUNCTION
 
FUNCTION isWilson(n, p)
LET FALSE = 0
LET TRUE = 1
 
IF p < n THEN LET isWilson = FALSE
LET prod = 1
LET p2 = p^2
FOR i = 1 TO n-1
LET prod = REMAINDER((prod*i), p2)
NEXT i
FOR i = 1 TO p-n
LET prod = REMAINDER((prod*i), p2)
NEXT i
LET prod = REMAINDER((p2+prod-(-1)^n), p2)
IF prod = 0 THEN LET isWilson = TRUE ELSE LET isWilson = FALSE
END FUNCTION
 
PRINT " n: Wilson primes"
PRINT "----------------------"
FOR n = 1 TO 11
PRINT USING "##: ": n;
FOR p = 3 TO 10099 STEP 2
IF isPrime(p) <> 0 AND isWilson(n, p) <> 0 THEN PRINT p; " ";
NEXT p
PRINT
NEXT n
END</syntaxhighlight>
 
==={{header|Visual Basic .NET}}===
Line 641 ⟶ 509:
11 | 17 2713
</pre>
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
func isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func is_wilson n p .
if p < n
return 0
.
prod = 1
p2 = p * p
for i = 1 to n - 1
prod = prod * i mod p2
.
for i = 1 to p - n
prod = prod * i mod p2
.
prod = (p2 + prod - pow -1 n) mod p2
if prod = 0
return 1
.
return 0
.
print "n: Wilson primes"
print "-----------------"
for n = 1 to 11
write n & " "
for p = 3 step 2 to 10099
if isprim p = 1 and is_wilson n p = 1
write p & " "
.
.
print ""
.
</syntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
Line 860 ⟶ 773:
3010 PROD# = PROD# - INT(PROD#/P2)*P2
3020 RETURN</syntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang="j"> wilson=. 0 = (*:@] | _1&^@[ -~ -~ *&! <:@[)^:<:
(>: i. 11x) ([ ;"0 wilson"0/ <@# ]) i.&.(p:inv) 11000
┌──┬───────────────┐
│1 │5 13 563 │
├──┼───────────────┤
│2 │2 3 11 107 4931│
├──┼───────────────┤
│3 │7 │
├──┼───────────────┤
│4 │10429 │
├──┼───────────────┤
│5 │5 7 47 │
├──┼───────────────┤
│6 │11 │
├──┼───────────────┤
│7 │17 │
├──┼───────────────┤
│8 │ │
├──┼───────────────┤
│9 │541 │
├──┼───────────────┤
│10│11 1109 │
├──┼───────────────┤
│11│17 2713 │
└──┴───────────────┘</syntaxhighlight>
 
=={{header|Java}}==
Line 1,086 ⟶ 1,027:
10: 11 1109
11: 17 2713</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
default("parisizemax", "1024M");
 
 
\\ Define the function wilsonprimes with a default limit of 11000
wilsonprimes(limit) = {
\\ Set the default limit if not specified
my(limit = if(limit, limit, 11000));
\\ Precompute factorial values up to the limit to save time
my(facts = vector(limit, i, i!));
\\ Sign variable for adjustment in the formula
my(sgn = 1);
print(" n: Wilson primes\n--------------------");
\\ Loop over the specified range (1 to 11 in the original code)
for(n = 1, 11,
print1(Str(" ", n, ": "));
sgn = -sgn; \\ Toggle the sign
\\ Loop over all primes up to the limit
forprime(p = 2, limit,
\\ Check the Wilson prime condition modified for PARI/GP
index=1;
if(n<2,index=1,index=n-1);
if(p > n && Mod(facts[index] * facts[p - n] - sgn, p^2) == 0,
print1(Str(p, " "));
)
);
print1("\n");
);
}
 
\\ Execute the function with the default limit
wilsonprimes();
</syntaxhighlight>
 
{{out}}
<pre>
n: Wilson primes
--------------------
1: 5 13 563
2: 3 11 107 4931
3: 7
4: 10429
5: 7 47
6: 11
7: 17
8:
9: 541
10: 11 1109
11: 17 2713
 
</pre>
 
=={{header|Perl}}==
Line 1,454 ⟶ 1,449:
11 │ 17 2,713
───────┴─────────────────────────────────────────────────────────────
</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP-49C}}
« → maxp
« { }
1 11 '''FOR''' n
{ } n
'''IF''' DUP ISPRIME? NOT '''THEN''' NEXTPRIME '''END'''
'''WHILE''' DUP maxp < '''REPEAT'''
n 1 - FACT OVER n - FACT * -1 n ^ -
'''IF''' OVER SQ MOD NOT '''THEN''' SWAP OVER + SWAP '''END'''
NEXTPRIME
'''END'''
DROP 1 →LIST +
'''NEXT'''
» » '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { { 5 13 } { 2 3 11 } { 7 } { } { 5 7 } { 11 } { 17 } { } { } { 11 } { 17 } }
</pre>
 
Line 1,490 ⟶ 1,505:
 
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
Line 1,607 ⟶ 1,623:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./big" for BigInt
import "./fmt" for Fmt
 
var limit = 11000
1,150

edits