Updated sieve example source

This commit is contained in:
Bobbi Webber-Manners 2018-05-03 21:15:14 -04:00 committed by GitHub
parent e52b236c53
commit 7cdaccab52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1097,47 +1097,49 @@ See `eightballvm.h` for technical details.
## Prime Number Sieve ## Prime Number Sieve
Here is the well-known Sieve of Eratosthenes algorithm for finding prime numbers, written in EightBall: Here is the well-known Sieve of Eratosthenes algorithm for finding prime numbers, written in EightBall:
```
' Sieve of Eratosthenes
' Sieve of Eratosthenes byte A[20*20] = 1
call doall(20, A)
' Globals end
byte nr = 10
word n = nr * nr sub doall(word nr, byte array[])
byte A[100] = 1 ' Has to be a literal word n = nr * nr
pr.msg "Sieve of Eratosthenes ..."
pr.msg "Sieve of Eratosthenes ..."; pr.nl pr.msg "nr is "; pr.dec nr; pr.nl
call doall() call sieve(n, nr, array)
end call printresults(n, array)
return 0
sub doall() endsub
call sieve()
call printresults() sub sieve(word n, word nr, byte AA[])
endsub pr.msg "Sieve"
word i = 0; word j = 0
sub sieve() for i = 2 : (nr - 1)
word i = 0; word j = 0 if AA[i]
for i = 2 : (nr - 1) j = i * i
if A[i] while (j < n)
j = i * i AA[j] = 0
while (j < n) j = j + i
A[j] = 0 endwhile
j = j + i endif
endwhile endfor
endif return 0
endfor endsub
endsub
sub printresults(word n, byte AA[])
sub printresults() word i = 0
word i = 0 for i = 2 : (n - 1)
for i = 2 : (n - 1) if AA[i]
if A[i] if i > 2
if i > 2 pr.msg ", "
pr.msg ", " endif
endif pr.dec i
pr.dec i endif
endif endfor
endfor pr.msg "."
pr.msg "." pr.nl
endsub return 0
endsub
```