prog8/examples/test.p8

52 lines
1.3 KiB
Plaintext
Raw Normal View History

%import c64utils
%zeropage basicsafe
2019-03-29 01:13:28 +00:00
~ main {
ubyte[256] sieve
ubyte candidate_prime = 2 ; is increased in the loop
2019-02-21 00:31:33 +00:00
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
while true {
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')
}
2019-04-04 19:02:24 +00:00
2019-06-23 00:49:37 +00:00
sub find_next_prime() -> ubyte {
2019-06-23 00:49:37 +00:00
while sieve[candidate_prime] {
candidate_prime++
if candidate_prime==0
return 0 ; we wrapped; no more primes available in the sieve
}
2019-06-23 00:49:37 +00:00
; found next one, mark the multiples and return it.
sieve[candidate_prime] = true
uword multiple = candidate_prime
2019-06-23 00:49:37 +00:00
2019-04-20 11:24:42 +00:00
while multiple < len(sieve) {
sieve[lsb(multiple)] = true
multiple += candidate_prime
; c64scr.print_uw(multiple) ; TODO
; c4.CHROUT('\n') ; TODO
}
return candidate_prime
}
}