'Subject: Wilson's theorem: iff p is prime (p - 1)! mod p == -1
'Author : Sjoerd.J.Schaper
'Date   : 12-06-2004
'Code   : QuickBasic, PDS, VBdos
REM $LANG: "qb"
REM $INCLUDE: 'largeint.bi'
CONST p = 0
vp = LargeInit(p, "wilsonth")
DIM g AS STRING
CLS : tim# = TIMER: lin = 8

begin:
LOCATE 1, 1
PRINT "   +------------------------------------------------+"
PRINT "   |  input odd p                                   |"
PRINT "   |  to test for primality with Wilson's theorem,  |"
PRINT "   |  0 to exit.                                    |"
PRINT "   |                                                |"
PRINT "   +------------------------------------------------+"
LOCATE 5, 7: INPUT ; "p"; g
IF INSTR(g, "'") THEN GOTO begin
IF VAL(g) < 2 THEN GOTO eind

vp = VAL(g)
IF (vp AND 1) = 0 THEN GOTO begin '   skip even p
LOCATE lin, 2
PRINT "p ="; vp

Fctl p, vp - 1 '                      compute factorial
IF NOT Isf(p, 0) THEN
   Inc p, 1
   g = " " + g + " is " '            (p - 1)! + 1 mod p
   IF Divint(p, vp) THEN g = g + "not "
   Prints g + "prime", 1 '            output test
END IF
Prints "", 1
lin = CSRLIN: GOTO begin

eind:
LOCATE lin, 1
g = STR$(CSNG(TIMER - tim#))
Prints "Timer:" + g + " s", 1
CALL Term
SYSTEM

