File tree Expand file tree Collapse file tree
project_euler/problem_025 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments