Skip to content

Instantly share code, notes, and snippets.

@yoshitsugu
Last active August 29, 2015 14:26
Show Gist options
  • Save yoshitsugu/c1612a074b0cc2752745 to your computer and use it in GitHub Desktop.
Save yoshitsugu/c1612a074b0cc2752745 to your computer and use it in GitHub Desktop.
Crystalでエラトステネスの篩
require "math/math"
def sieve(max : Int32, tmps : Array(Int32), results: Array(Int32)) : Array(Int32)
tmp = tmps.first
return (results + tmps) if tmp > Math.sqrt(max).floor
sieve(max, tmps.select{|c| c % tmp != 0}, results << tmp)
end
def primaries (max : Int32) : Array(Int32)
sieve(max, (2..max).to_a, [] of Int32)
end
p primaries 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment