' Sieve of Eratosthenes

pr.msg "Sieve of Eratosthenes ..."

const sz=30
const arrsz=sz*sz
byte A[arrsz] = {}
word i = 0
for i = 0 : arrsz-1
  A[i] = 1
endfor
call doall(sz, A)
end

sub doall(word nr, byte array[])
  word n = nr * nr
  pr.msg "nr is "; pr.dec nr; pr.nl
  call sieve(n, nr, array)
  call printresults(n, array)
  return 0
endsub

sub sieve(word n, word nr, byte AA[])
  pr.msg "Sieve"
  word i = 0; word j = 0
  for i = 2 : (nr - 1)
    if AA[i]
      j = i * i
      while (j < n)
        AA[j] = 0
        j = j + i
      endwhile
    endif
  endfor
  return 0
endsub

sub printresults(word n, byte AA[])
  word i = 0
  for i = 2 : (n - 1)
    if AA[i]
      if i > 2
        pr.msg ", "
      endif
      pr.dec i
    endif
  endfor
  pr.msg "."
  pr.nl
  return 0
endsub

