-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path050.rb
42 lines (36 loc) · 782 Bytes
/
050.rb
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
max_value = 1000000
sieve = []
(2..max_value).each { |i|
sieve << i
}
sieve.each { |index|
next if index.nil?
value = index
(value..max_value).step(value) { |filter|
sieve[filter-2] = nil if filter != value
}
}
sieve.compact!
hashed_sieve = {}
sieve.each do |prime|
hashed_sieve[prime] = true
end
best_consecutive = 0
best_prime = 0
sieve.each_with_index do |prime, index|
new_number = prime
new_consecutive = 1
index += 1
while new_number < 1000000 and !sieve[index].nil?
new_number += sieve[index]
if hashed_sieve.include?(new_number)
if new_consecutive > best_consecutive
best_consecutive = new_consecutive
best_prime = new_number
end
end
new_consecutive += 1
index += 1
end
end
puts best_prime