Created
January 21, 2014 02:01
-
-
Save jamesjtong/8533129 to your computer and use it in GitHub Desktop.
euler 10
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
def summation_of_primes(max) | |
array_of_nums = (0...max).to_a | |
array_of_nums[0] = nil | |
array_of_nums[1] = nil | |
#skip index 1 as thats number 1 and don't want to add that in later, 0 by default wont be added in | |
prod = 1 | |
array_of_nums.each do |num| | |
next if num == nil | |
until prod > max | |
(2..max).each do |mult| | |
prod = num * mult | |
array_of_nums[prod] = nil | |
end | |
end | |
end | |
p array_of_nums.compact.inject(&:+) | |
end | |
summation_of_primes(2000000) | |
#1000000000001 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment