Last active
August 29, 2015 14:26
-
-
Save yoshitsugu/c1612a074b0cc2752745 to your computer and use it in GitHub Desktop.
Crystalでエラトステネスの篩
This file contains hidden or 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
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