For example:
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
# Find the sum of all the primes below two million.
def prime_array
numbers = (2..7).to_a
divisor = 2
while divisor<10
numbers.each do |x|
numbers.delete(x) if x%divisor == 0 && x != divisor
end
divisor += 1
end
return numbers
end
def sum_of_primes
sum = 0
prime_array.each do |x|
sum += x
end
return sum
end
puts sum_of_primes
# This takes forever to calculateI put this problem aside for awhile and kept cranking through the problem set until I hit big one: The Grid Problem.
This is a fun one. I've run into a few stumbling blocks, but I'm hoping to finish it tomorrow.
No comments:
Post a Comment