Skip to content

Instantly share code, notes, and snippets.

@jamesjtong
Created January 21, 2014 02:01
Show Gist options
  • Save jamesjtong/8533129 to your computer and use it in GitHub Desktop.
Save jamesjtong/8533129 to your computer and use it in GitHub Desktop.
euler 10
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