Skip to content

Instantly share code, notes, and snippets.

@simonask
Created February 6, 2009 20:53
Show Gist options
  • Select an option

  • Save simonask/59605 to your computer and use it in GitHub Desktop.

Select an option

Save simonask/59605 to your computer and use it in GitHub Desktop.
PrimeGenerator for new, experimental Snow syntax
-------------8<-----------------
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