spinix-hive/devel/prime.spn

63 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

con
_clkmode = $62
_clkfreq = $4E495053 ' SPIN
obj
c : "clibsd"
dat
primes long $00020001, 0, 0, 0, 0, 0, 0, 0, 0, 0
long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
last long 0
pub main(argc, argv) | i, j, n, num, maxnum, count, prime
c.enter(argc, argv)
if argc <> 2
c.printf0(string("usage: prime max_number\n"))
c.exit(1)
maxnum := 0
i := long[argv][1]
repeat while byte[i]
maxnum := (maxnum * 10) + byte[i++] - "0"
c.printf1(string("%d "), 1)
c.printf1(string("%d "), 2)
num := (@last - @primes)/2
n := 2
i := 3
count := 2
repeat while i < maxnum
j := 2
repeat while j < n
prime := word[@primes][j]
if prime * prime > i
j := n
quit
if i / prime * prime == i
quit
j++
if j == n
if n < num
word[@primes][n++] := i
if n == num and i * i < maxnum
maxnum := i * i
c.printf1(string("%d "), i)
if ++count => 10
count := 0
c.printf0(string("\n"))
i += 2
if count
c.printf0(string("\n"))
c.exit(0)