main
1require 'spec_helper'
2require 'prime'
3
4describe "problem ten" do
5 #The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
6
7 #Find the sum of all the primes below two million.
8
9 class SumOfPrime
10 attr_reader :limit
11
12 def initialize(limit)
13 @limit = limit
14 end
15
16 def sum
17 each.reduce(:+)
18 end
19
20 def each
21 Prime.each(limit)
22 #Enumerator.new do |yielder|
23 #2.upto(limit) do |n|
24 #yielder.yield(n) if prime?(n)
25 #end
26 #end
27 end
28
29 private
30
31 def prime?(number)
32 (2...number).each do |n|
33 return false if number % n == 0
34 end
35 true
36 end
37 end
38
39 it 'returns the sum of the primes below 10' do
40 expect(SumOfPrime.new(10).sum).to eql(17)
41 end
42
43 it 'returns thesum of the primes below 2_000_000' do
44 expect(SumOfPrime.new(2_000_000).sum).to eql(142_913_828_922)
45 end
46end