mirror of
https://github.com/bobbimanners/EightBall.git
synced 2024-11-22 12:34:23 +00:00
Created Sieve of Eratosthenes (markdown)
parent
8ed96582b3
commit
9efaf6516b
45
Sieve-of-Eratosthenes.md
Normal file
45
Sieve-of-Eratosthenes.md
Normal file
@ -0,0 +1,45 @@
|
||||
Here is the well-known Sieve of Eratosthenes algorithm for finding prime numbers, written in EightBall:
|
||||
|
||||
' Sieve of Eratosthenes
|
||||
|
||||
' Globals
|
||||
byte nr = 10
|
||||
word n = nr * nr
|
||||
byte A[n] = 1
|
||||
|
||||
pr.msg "Sieve of Eratosthenes ..."; pr.nl
|
||||
call doall()
|
||||
end
|
||||
|
||||
sub doall()
|
||||
call sieve()
|
||||
call printresults()
|
||||
return 0
|
||||
|
||||
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
|
||||
return 0
|
||||
|
||||
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 "."
|
||||
return 0
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user