mirror of
https://github.com/bobbimanners/EightBall.git
synced 2025-02-20 08:28:56 +00:00
Updated sieve example source
This commit is contained in:
parent
e52b236c53
commit
7cdaccab52
88
README.md
88
README.md
@ -1097,47 +1097,49 @@ See `eightballvm.h` for technical details.
|
||||
## Prime Number Sieve
|
||||
|
||||
Here is the well-known Sieve of Eratosthenes algorithm for finding prime numbers, written in EightBall:
|
||||
```
|
||||
' Sieve of Eratosthenes
|
||||
|
||||
' Sieve of Eratosthenes
|
||||
|
||||
' Globals
|
||||
byte nr = 10
|
||||
word n = nr * nr
|
||||
byte A[100] = 1 ' Has to be a literal
|
||||
|
||||
pr.msg "Sieve of Eratosthenes ..."; pr.nl
|
||||
call doall()
|
||||
end
|
||||
|
||||
sub doall()
|
||||
call sieve()
|
||||
call printresults()
|
||||
endsub
|
||||
|
||||
sub 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
|
||||
endsub
|
||||
|
||||
sub printresults()
|
||||
word i = 0
|
||||
for i = 2 : (n - 1)
|
||||
if A[i]
|
||||
if i > 2
|
||||
pr.msg ", "
|
||||
endif
|
||||
pr.dec i
|
||||
endif
|
||||
endfor
|
||||
pr.msg "."
|
||||
endsub
|
||||
|
||||
|
||||
byte A[20*20] = 1
|
||||
call doall(20, A)
|
||||
end
|
||||
|
||||
sub doall(word nr, byte array[])
|
||||
word n = nr * nr
|
||||
pr.msg "Sieve of Eratosthenes ..."
|
||||
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
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user