'Subject: Lucas-Lehmer test for Mersenne numbers 2^ p - 1,
'         with odd prime p. Toy version for small p.
'Author : Sjoerd.J.Schaper
'Date   : 12-06-2004
'Code   : QuickBasic, PDS, VBdos
REM $LANG: "qb"
REM $INCLUDE: 'largeint.bi'
CONST p = 0, s = 1
k = LargeInit(s, "mersenne")
CLS : tim# = TIMER: lin = 7

begin:
LOCATE 1, 1
PRINT "   +---------------------------+"
PRINT "   |  input prime exponent p,  |"
PRINT "   |  0 to exit.               |"
PRINT "   |                           |"
PRINT "   +---------------------------+"
LOCATE 4, 7: INPUT ; "p"; ip$
IF INSTR(ip$, "'") THEN GOTO begin
ep = CINT(VAL(LEFT$(ip$, 5)))
IF ep < 1 THEN GOTO eind
IF ep > 30703 THEN GOTO begin
IF (ep AND 1) = 0 THEN GOTO begin '   skip even p

LOCATE lin, 2
PRINT "p ="; ep

Letf s, 4: Letf p, 1 '                initialize s,
Lsft p, ep: Inc p, -1 '               compute power - 1
FOR k = 2 TO ep - 1 '                 calculate Lucas series
   Squ s, t1: Inc t1, -2
   Dup t1, s: Boolf s, p, 1
   Rsft t1, ep: Add s, t1 '           Mod 2^ p - 1
   IF Cmp(s, p) >= 0 THEN Subt s, p
NEXT k

g$ = " 2^ " + ip$ + " - 1 is " '      output test
IF NOT Isf(s, 0) THEN g$ = g$ + "not "
Prints g$ + "prime", 2
lin = CSRLIN
GOTO begin

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

