User guide

214 CHAPTER 13. EXAMPLE PROGRAMS
13.2 Primes
The following program prints out a table of all primes less than 1000, using the sie ve
method.
GET "libhdr"
GLOBAL { count: ug }
MANIFEST { upb = 999 }
LET start() = VALOF
{ LET isprime = getvec(upb)
count := 0
FOR i = 2 TO upb DO isprime!i := TRUE // Until proved otherwise.
FOR p = 2 TO upb IF isprime!p DO
{ LET i = p*p
UNTIL i>upb DO { isprime!i := FALSE; i := i + p }
out(p)
}
writes("*nend of output*n")
freevec(isprime)
RESULTIS 0
}
AND out(n) BE
{ IF count REM 10 = 0 DO newline()
writef(" %i3", n)
count := count + 1
}
13.3 Queens
The following program calculates the number of ways n queens can be placed on a n×n
chess board without any two occupying the same row, column or diagonal.
GET "libhdr"
GLOBAL { count:200; all:201 }
LET try(ld, col, rd) BE TEST col=all
THEN count := count + 1
ELSE { LET poss = all & ~(ld | col | rd)
UNTIL poss=0 DO
{ LET p = poss & -poss
poss := poss - p
try(ld+p << 1, col+p, rd+p >> 1)
}
}