#The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. #Find the sum of all the primes below two million. # require 'mathn' def sum_of_primes_below_two_million gen = Prime.new sum = 0 gen.each_with_index do |g, index| sum += g break if index == 200_0000_0000 end puts sum end def sum_of_primes d = 2 sum = [] 2.upto(200_0000) do |i| while(d<=i) if(i%d == 0) i = i/d sum.push(d) else d +=1 end end d = 2 end puts sum.uniq.inject(0){ |s,i|s+=i } end sum_of_primes #sum_of_primes_below_two_million
ProjectRuler-10
最新推荐文章于 2011-11-04 12:45:14 发布