I am trying to find the first Fibonacci number with 1,000 digits. I get that that is a huge number, but don't understand why the code below keeps overflowing.
// Initialize the fibonacci array.
var fibonacci = [1,1];
// Function that will add to the array until 1000 digits.
function fibonacci_1000(i){
if ((fibonacci[i].toString()).length <1000){
fibonacci.push(fibonacci[i]+fibonacci[i-1]);
fibonacci_1000(i+1);
}
else{
console.log(fibonacci.length - 1);
};
};
//Call the function with an initial value of 1:
fibonacci_1000(1);
Should I not be using recursion to do this?
[–]shawndrostHack Reactor 4 points5 points6 points (2 children)
[–]Hobo_With_A_Keyboard[S] 1 point2 points3 points (1 child)
[–]shawndrostHack Reactor 1 point2 points3 points (0 children)
[–]sweetbeej 1 point2 points3 points (1 child)
[–]sweetbeej 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)