0

I need to make a program to find the nth Fibonacci number and here is my code:

fib,num2 = 0,1
for i in range(int(input())):
    fib,num2 = num2,fib+num2
print(fib)

Some of the cases my program needs to do in less than 2 seconds are as big as 10^19.

How would I code that?

Mehdi Alali
  • 163
  • 1
  • 11

1 Answers1

0

I had similar problem, i am not sure but you can try memoization

var fib3 = (function(){
    var memo = {};
    return function(n) {
        if (memo[n]) {return memo[n];}
        return memo[n] = (n <= 2) ? 1 : fib3(n-2) + fib3(n-1);
    };
})();