procedure main(arg)
limit := integer(arg[1]) | 30
every write(genprime(limit)) # typical usage
end
procedure genprime(limit)
# This is a list of 14 fractions that encode a
# FRACTRAN program for computing prime numbers.
f := list(14)
f[ 1] := fract(17,91)
f[ 2] := fract(78,85)
f[ 3] := fract(19,51)
f[ 4] := fract(23,38)
f[ 5] := fract(29,33)
f[ 6] := fract(77,29)
f[ 7] := fract(95,23)
f[ 8] := fract(77,19)
f[ 9] := fract( 1,17)
f[10] := fract(11,13)
f[11] := fract(13,11)
f[12] := fract(15, 2)
f[13] := fract( 1, 7)
f[14] := fract(55, 1)
# Construct a table of the powers of two.
pot := table()
power := 1
every n := 0 to limit - 1 do {
pot[power] := n
power *:= 2
}
# Generate the primes.
s := 2
repeat {
# Multiply s by the first fraction that produces an
# integer result.
x := !f & (s * x.numer) % x.denom = 0
s := (s * x.numer) / x.denom
# The exponent is prime if s is a power of two;
# if so, produce it.
suspend \pot[s]
}
end