Created
April 25, 2018 02:05
-
-
Save jmiven/914f415eef891adfc628cbbcdab6189c to your computer and use it in GitHub Desktop.
Concurrent prime number sieve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Finds prime numbers up to the value of the first argument or 100. | |
// Terribly inefficient but fun. | |
// Source: http://www.cs.dartmouth.edu/~doug/sieve/sieve.pdf | |
actor Main | |
let max: I32 | |
let sink: Prime | |
new create(env: Env) => | |
max = try env.args(1)?.i32()? else 100 end | |
let n: I32 = 2 | |
sink = Prime(env, n) | |
send(n) | |
be send(n: I32) => | |
if n < max then | |
sink.filter(n) | |
send(n + 1) | |
end | |
actor Prime | |
let p: I32 | |
var sink: (Prime | None) | |
let env: Env | |
new create(env': Env, p': I32) => | |
p = p' | |
sink = None | |
env = env' | |
env.out.print(p.string()) | |
be filter(n : I32) => | |
if (n % p) != 0 then | |
match sink | |
| None => sink = Prime(env, n) | |
| let q: Prime => q.filter(n) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment