How to resolve the algorithm Primality by Wilson's theorem step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Primality by Wilson's theorem step by step in the BASIC programming language
Table of Contents
Problem Statement
Write a boolean function that tells whether a given integer is prime using Wilson's theorem. By Wilson's theorem, a number p is prime if and only if p divides (p - 1)! + 1. Remember that 1 and all non-positive integers are not prime.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Primality by Wilson's theorem step by step in the BASIC programming language
Source code in the basic programming language
100 HOME : REM 100 CLS for Chipmunk Basic
110 PRINT "Primes below 100"+CHR$(10)
120 FOR n = 2 TO 100
130 GOSUB 160
140 NEXT n
150 END
160 rem FUNCTION WilsonPrime(n)
170 fct = 1
180 FOR i = 2 TO n-1
181 a = fct * i
190 fct = a - INT(a / n) * n
200 NEXT i
210 IF fct = n-1 THEN PRINT i;" ";
220 RETURN
function wilson_prime(n)
fct = 1
for i = 2 to n-1
fct = (fct * i) mod n
next i
if fct = n-1 then return True else return False
end function
print "Primes below 100" & Chr(10)
for i = 2 to 100
if wilson_prime(i) then print i; " ";
next i
end
100 cls
110 print "Primes below 100"+chr$(10)
120 for i = 2 to 100
130 wilsonprime(i)
140 next i
150 end
160 function wilsonprime(n)
170 fct = 1
180 for i = 2 to n-1
190 fct = (fct*i) mod n
200 next i
210 if fct = n-1 then print i;
220 end function
for i = 2 to 100
let f = 1
for j = 2 to i - 1
let f = (f * j) % i
wait
next j
if f = i - 1 then
print i
endif
next i
end
100 CLS : REM 100 CLS for Chipmunk Basic
110 PRINT "Primes below 100"+CHR$(10)
120 FOR N = 2 TO 100
130 GOSUB 160
140 NEXT N
150 END
160 REM FUNCTION WilsonPrime(n)
170 FCT = 1
180 FOR I = 2 TO N-1
190 FCT = (FCT*I) MOD N
200 NEXT I
210 IF FCT = N-1 THEN PRINT I;" ";
220 RETURN
110 PRINT "Primes below 100"
120 FOR n = 2 TO 100
130 GOSUB 160
140 NEXT n
150 GOTO 250
160 rem FUNCTION WilsonPrime(n)
170 LET f = 1
180 FOR i = 2 TO n-1
181 LET a = f * i
190 LET f = a - INT(a / n) * n
200 NEXT i
210 IF f = n-1 THEN 230
220 RETURN
230 PRINT i
240 RETURN
250 END
FUNCTION wilsonprime(n)
fct = 1
FOR i = 2 TO n - 1
fct = (fct * i) MOD n
NEXT i
IF fct = n - 1 THEN wilsonprime = 1 ELSE wilsonprime = 0
END FUNCTION
PRINT "Primes below 100"; CHR$(10)
FOR i = 2 TO 100
IF wilsonprime(i) THEN PRINT i; " ";
NEXT i
100 CLS
110 PRINT "Primes below 100": PRINT
120 FOR n = 2 TO 100
130 GOSUB 160
140 NEXT n
150 GOTO 250
160 rem FUNCTION WilsonPrime(n)
170 LET f = 1
180 FOR i = 2 TO n-1
181 LET a = f * i
190 LET f = a - INT(a / n) * n
200 NEXT i
210 IF f = n-1 THEN 230
220 RETURN
230 PRINT i;" ";
240 RETURN
250 END
Procedure wilson_prime(n.i)
fct.i = 1
For i.i = 2 To n-1
fct = (fct * i) % n
Next i
If fct = n-1
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
OpenConsole()
PrintN("Primes below 100")
For i = 2 To 100
If wilson_prime(i)
Print(Str(i) + #TAB$)
EndIf
Next i
PrintN("")
Input()
CloseConsole()
print "Primes below 100"
for i = 2 to 100
if wilsonprime(i) = 1 then print i; " ";
next i
end
function wilsonprime(n)
fct = 1
for i = 2 to n-1
fct = (fct * i) mod n
next i
if fct = n-1 then wilsonprime = 1 else wilsonprime = 0
end function
FUNCTION wilsonprime(n)
LET fct = 1
FOR i = 2 TO n - 1
LET fct = MOD((fct * i), n)
NEXT i
IF fct = n - 1 THEN LET wilsonprime = 1 ELSE LET wilsonprime = 0
END FUNCTION
PRINT "Primes below 100"; CHR$(10)
FOR i = 2 TO 100
IF wilsonprime(i) = 1 THEN PRINT i; " ";
NEXT i
END
print "Primes below 100\n"
for i = 2 to 100
if wilson_prime(i) print i, " ";
next i
sub wilson_prime(n)
local i, fct
fct = 1
for i = 2 to n-1
fct = mod((fct * i), n)
next i
if fct = n-1 then return True else return False : fi
end sub
You may also check:How to resolve the algorithm Function frequency step by step in the Wren programming language
You may also check:How to resolve the algorithm Exponentiation order step by step in the REXX programming language
You may also check:How to resolve the algorithm Literals/String step by step in the XSLT programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Sleep step by step in the REXX programming language