Skip to content

Commit 6317f2b

Browse files
authored
Merge pull request #204 from domix80/problem_025
Feat: Project Euler Problem 25
2 parents 9346d99 + 1dbf679 commit 6317f2b

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

project_euler/problem_025/sol1.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#The Fibonacci sequence is defined by the recurrence relation:
2+
# Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
3+
#Hence the first 12 terms will be:
4+
#
5+
# F1 = 1
6+
# F2 = 1
7+
# F3 = 2
8+
# F4 = 3
9+
# F5 = 5
10+
# F7 = 13
11+
# F8 = 21
12+
# F6 = 8
13+
# F9 = 34
14+
# F10 = 55
15+
# F11 = 89
16+
# F12 = 144
17+
#
18+
#The 12th term, F12, is the first term to contain three digits.
19+
#What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
20+
21+
def solution(num_digits = 1000)
22+
#Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
23+
resultn1 = 1
24+
resultn2 = 1
25+
result = 2
26+
index = 3
27+
value = true
28+
while value
29+
resultn2 = resultn1
30+
resultn1 = result
31+
if (resultn1 + resultn2).abs.digits.length < num_digits
32+
value = true
33+
else
34+
value = false
35+
end
36+
result = resultn1 + resultn2
37+
index += 1
38+
end
39+
res = index
40+
end
41+
42+
answer = solution()
43+
p answer
44+

0 commit comments

Comments
 (0)