Wilson primes of order n: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
(Wilson primes of order n in Applesoft BASIC, Chipmunk Basic and MSX Basic)
Line 91: Line 91:


=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{trans|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">100 home
110 print "n: Wilson primes"
120 print "--------------------"
130 for n = 1 to 11
140 print n;chr$(9);
150 for p = 2 to 18
160 gosub 240
170 if pt = 0 then goto 200
180 gosub 340
190 if wnpt = 1 then print p,
200 next p
210 print
220 next n
230 end
240 rem tests if the number P is prime
250 rem result is stored in PT
260 pt = 1
270 if p = 2 then return
280 if p * 2 - int(p / 2) = 0 then pt = 0 : return
290 j = 3
300 if j*j > p then return
310 if p * j - int(p / j) = 0 then pt = 0 : return
320 j = j+2
330 goto 300
340 rem tests if the prime p is a Wilson prime of order n
350 rem make sure it actually is prime first
360 rem result is stored in wnpt
370 wnpt = 0
380 if p = 2 and n = 2 then wnpt = 1 : return
390 if n > p then wnpt = 0 : return
400 prod = 1 : p2 = p*p
410 for i = 1 to n-1
420 prod = (prod*i) : gosub 500
430 next i
440 for i = 1 to p-n
450 prod = (prod*i) : gosub 500
460 next i
470 prod = (p2+prod-(-1)^n) : gosub 500
480 if prod = 0 then wnpt = 1 : return
490 wnpt = 0 : return
500 rem prod mod p2 fails if prod > 32767 so brew our own modulus function
510 prod = prod-int(prod/p2)*p2
520 return</syntaxhighlight>

==={{header|BASIC256}}===
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
Line 125: Line 171:
next n
next n
end</syntaxhighlight>
end</syntaxhighlight>

==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|GW-BASIC}}
{{works with|MSX_BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{trans|GW-BASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 print "n: Wilson primes"
120 print "--------------------"
130 for n = 1 to 11
140 print n;chr$(9);
150 for p = 2 to 18
160 gosub 240
170 if pt = 0 then goto 200
180 gosub 340
190 if wnpt = 1 then print p,
200 next p
210 print
220 next n
230 end
240 rem tests if the number P is prime
250 rem result is stored in PT
260 pt = 1
270 if p = 2 then return
280 if p mod 2 = 0 then pt = 0 : return
290 j = 3
300 if j*j > p then return
310 if p mod j = 0 then pt = 0 : return
320 j = j+2
330 goto 300
340 rem tests if the prime p is a Wilson prime of order n
350 rem make sure it actually is prime first
360 rem result is stored in wnpt
370 wnpt = 0
380 if p = 2 and n = 2 then wnpt = 1 : return
390 if n > p then wnpt = 0 : return
400 prod = 1 : p2 = p*p
410 for i = 1 to n-1
420 prod = (prod*i) : gosub 500
430 next i
440 for i = 1 to p-n
450 prod = (prod*i) : gosub 500
460 next i
470 prod = (p2+prod-(-1)^n) : gosub 500
480 if prod = 0 then wnpt = 1 : return
490 wnpt = 0 : return
500 rem prod mod p2 fails if prod > 32767 so brew our own modulus function
510 prod = prod-int(prod/p2)*p2
520 return</syntaxhighlight>


==={{header|QBasic}}===
==={{header|QBasic}}===
Line 165: Line 262:
NEXT n
NEXT n
END</syntaxhighlight>
END</syntaxhighlight>

==={{header|MSX Basic}}===
Both the [[#GW-BASIC|GW-BASIC]] and [[#Chipmunk_Basic|Chipmunk Basic]] solutions work without change.


==={{header|Yabasic}}===
==={{header|Yabasic}}===