RosettaCodeData/Task/Primality-by-Wilsons-theorem/Tiny-BASIC/primality-by-wilsons-theorem.basic
2023-07-01 13:44:08 -04:00

26 lines
667 B
Text

PRINT "Number to test"
INPUT N
IF N < 0 THEN LET N = -N
IF N = 2 THEN GOTO 30
IF N < 2 THEN GOTO 40
LET F = 1
LET J = 1
10 LET J = J + 1
REM exploits the fact that (F mod N)*J = (F*J mod N)
REM to do the factorial without overflowing
LET F = F * J
GOSUB 20
IF J < N - 1 THEN GOTO 10
IF F = N - 1 THEN PRINT "It is prime"
IF F <> N - 1 THEN PRINT "It is not prime"
END
20 REM modulo by repeated subtraction
IF F < N THEN RETURN
LET F = F - N
GOTO 20
30 REM special case N=2
PRINT "It is prime"
END
40 REM zero and one are nonprimes by definition
PRINT "It is not prime"
END