Skip to content

Commit b7e9cfd

Browse files
authored
Create 025.js
1 parent 7d4ed45 commit b7e9cfd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

025.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//function that returns F_n where F_1 = 1, F_2 = 1, F_3 = 2, F_4 = 3, etc.
2+
function fibonacci(n) {
3+
let fibonacci = [1n, 1n]; //create an array of first 2 terms in the sequence
4+
5+
while (fibonacci.length < n) {
6+
fibonacci.push(fibonacci[BigInt(fibonacci.length)-1n] + fibonacci[BigInt(fibonacci.length)-2n]) //push the sum of the last 2 terms, because that's the fibonacci sequence rule
7+
}
8+
9+
return fibonacci[BigInt(fibonacci.length)-1n]; //return the last element of the array since it's the nth fibonacci number
10+
}
11+
12+
let index = 1n;
13+
14+
while (fibonacci(index).toString().length < 1000) { //keep generating fibonacci numbers until there's one that's 1000 digits long
15+
index++;
16+
}
17+
18+
console.log(index); //outputs answer

0 commit comments

Comments
 (0)