You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* The Fibonacci sequence is defined by the recurrence relation:
7
+
*
8
+
* Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
9
+
*
10
+
* Hence the first 12 terms will be:
11
+
*
12
+
* F1 = 1
13
+
* F2 = 1
14
+
* F3 = 2
15
+
* F4 = 3
16
+
* F5 = 5
17
+
* F6 = 8
18
+
* F7 = 13
19
+
* F8 = 21
20
+
* F9 = 34
21
+
* F10 = 55
22
+
* F11 = 89
23
+
* F12 = 144
24
+
* The 12th term, F12, is the first term to contain three digits.
25
+
26
+
* What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
27
+
*/
28
+
29
+
// brute force method
30
+
31
+
functionfibonacci(n){
32
+
// Creates an array of Fibonacci numbers using the Fibonacci formula. Returns the nth element of the array.
33
+
if(n===1){
34
+
return0
35
+
}
36
+
elseif(n===2){
37
+
return1
38
+
}
39
+
else{
40
+
series=[0,1]
41
+
for(leti=2;i<=n;i++){
42
+
series.push(sequence[i-1]+sequence[i-2])
43
+
}
44
+
returnsequence[n]
45
+
}
46
+
}
47
+
48
+
functionfibonacciIndex(n=1000){
49
+
// Computes incrementing Fibonacci numbers starting from 3 and checks if its length is equal to n. Returns the term of the sequence in which it happens first.
0 commit comments