EightBall/sieve4.8b
Bobbi Webber-Manners cdbfeafda9
v0.52. Fix memory corruption bug.
This fixes a memory corruption bug that occurred when compiling code with subroutines.  The symbol table was corrupting the source code.  This was showing up on 6502 systems only (seemed okay on Linux).
2018-04-28 14:00:56 -04:00

48 lines
657 B
Plaintext

' Sieve of Eratosthenes
byte A[400] = 1
call doall(20)
end
sub doall(word nr)
word n = nr * nr
pr.msg "Sieve of Eratosthenes ..."
pr.msg "nr is "; pr.dec nr; pr.nl
call sieve(n, nr)
call printresults(n)
return 0
endsub
sub sieve(word n, word nr)
pr.msg "Sieve"
word i = 0; word j = 0
for i = 2 : (nr - 1)
if A[i]
j = i * i
while (j < n)
A[j] = 0
j = j + i
endwhile
endif
endfor
return 0
endsub
sub printresults(word n)
word i = 0
for i = 2 : (n - 1)
if A[i]
if i > 2
pr.msg ", "
endif
pr.dec i
endif
endfor
pr.msg "."
pr.nl
return 0
endsub