Skip to content

Commit e8105e4

Browse files
committed
Feat: Project Euler Problem 25
1 parent e2f6bd9 commit e8105e4

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_021/sol1.rb)
160160
* Problem 022
161161
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_022/sol1.rb)
162+
* Problem 025
163+
* [Sol1](https://github.com/TheAlgorithms/Ruby/blob/master/project_euler/problem_025/sol1.rb)
162164

163165
## Searches
164166
* [Binary Search](https://github.com/TheAlgorithms/Ruby/blob/master/searches/binary_search.rb)

project_euler/problem_025/sol1.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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?()
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+
num_digits = 1000
28+
value = true
29+
while value
30+
resultn2 = resultn1
31+
resultn1 = result
32+
if (resultn1 + resultn2).abs.digits.length < num_digits
33+
value = true
34+
else
35+
value = false
36+
end
37+
result = resultn1 + resultn2
38+
index += 1
39+
end
40+
res = index
41+
end
42+
43+
answer = solution?()
44+
p answer
45+

0 commit comments

Comments
 (0)