forked from rab/euler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.rb
More file actions
executable file
·42 lines (35 loc) · 808 Bytes
/
10.rb
File metadata and controls
executable file
·42 lines (35 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/ruby -w
#
# Problem 10
# 08 February 2002
#
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
#
# Find the sum of all the primes below two million.
puts 'Sieve is more efficient than Prime for primes of known size'
require 'sieve'
puts Sieve.upto(2_000_000).inject(0) {|p,sum|sum+p}
# require 'primes'
# puts Prime.to(2_000_000).inject(0) {|p,sum|sum+p}
# require 'mathn'
# primes = Prime.new
# sum = 0
# while (p = primes.next) < 2_000_000
# sum += p
# end
# puts sum
__END__
if $0 == __FILE__
require 'test/unit'
class Euler10Test < Test::Unit::TestCase
def test_sum_under_10
expected = 17
actual = 0
primes = Prime.new
while (p = primes.next) < 10
actual += p
end
assert_equal expected, actual
end
end
end