mirror of
https://github.com/irmen/prog8.git
synced 2024-11-01 00:10:48 +00:00
62 lines
1.4 KiB
Lua
62 lines
1.4 KiB
Lua
%import c64utils
|
|
%zeropage basicsafe
|
|
|
|
main {
|
|
|
|
ubyte[256] sieve
|
|
ubyte candidate_prime = 2 ; is increased in the loop
|
|
|
|
sub start() {
|
|
memset(sieve, 256, false) ; clear the sieve, to reset starting situation on subsequent runs
|
|
|
|
; calculate primes
|
|
c64scr.print("prime numbers up to 255:\n\n")
|
|
ubyte amount=0
|
|
forever {
|
|
ubyte prime = find_next_prime()
|
|
if prime==0
|
|
break
|
|
c64scr.print_ub(prime)
|
|
c64scr.print(", ")
|
|
amount++
|
|
}
|
|
c64.CHROUT('\n')
|
|
c64scr.print("number of primes (expected 54): ")
|
|
c64scr.print_ub(amount)
|
|
c64.CHROUT('\n')
|
|
|
|
check_eval_stack()
|
|
}
|
|
|
|
|
|
sub find_next_prime() -> ubyte {
|
|
|
|
while sieve[candidate_prime] {
|
|
candidate_prime++
|
|
if candidate_prime==0
|
|
return 0 ; we wrapped; no more primes available in the sieve
|
|
}
|
|
|
|
; found next one, mark the multiples and return it.
|
|
sieve[candidate_prime] = true
|
|
uword multiple = candidate_prime
|
|
|
|
|
|
while multiple < len(sieve) {
|
|
sieve[lsb(multiple)] = true
|
|
multiple += candidate_prime
|
|
}
|
|
return candidate_prime
|
|
}
|
|
|
|
|
|
|
|
sub check_eval_stack() {
|
|
if X!=255 {
|
|
c64scr.print("stack x=")
|
|
c64scr.print_ub(X)
|
|
c64scr.print(" error!\n")
|
|
}
|
|
}
|
|
}
|