-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_2.rb
39 lines (33 loc) · 1 KB
/
euler_2.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
#euler #2 in ruby
#find the sum of the even valued terms of the fibonnaci sequence that are less than N
#inputs : T (# of test cases / queries)
# N (see the above)
#output : blah , it's a sum
#consider making this more usable for other situations,
#example: add functionality for finding the nth term of the fibonnaci sequence (use the even fibs to cut down on runtime)
def nextEvenFib(current, previous)
3.times do
temp = current
current = current + previous
previous = temp
end
result = [current, previous]
end
evenSums = [ 2, 8 ]
fibInjectors = [ 1, 5 ]
t = gets.strip.to_i
for a0 in (0..t-1)
n = gets.strip.to_i
total = 0
#check to see if we've ventured that far into the fib sequence yet
while evenSums.last < n
temp = nextEvenFib(evenSums.last, fibInjectors.last)
evenSums << temp[0]
fibInjectors << temp[1]
end
evenSums.each do |number|
break if number >= n
total = total + number
end
puts total
end