Skip to content

Instantly share code, notes, and snippets.

@simonask
Created February 3, 2009 21:25
Show Gist options
  • Save simonask/57771 to your computer and use it in GitHub Desktop.
Save simonask/57771 to your computer and use it in GitHub Desktop.
PrimeGenerator: {
return [idx] {
.cache: .cache? || @
cached: .cache(idx)
return cached if cached
max: .cache.last || 2
is_prime: [n] {
for x in range(2, Math.sqrt(n))
for y in range(2, Math.sqrt(x))
return false if x*y = n
end
end
return true
}
candidate: max + 1
candidate +: 1 while not is_prime(candidate)
.cache.set(idx, candidate)
return candidate
}
}
FirstPrime, SecondPrime, ThirdPrime: PrimeGenerator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment