I have a problem and I have to write a program (in JavaScript) to return the n-th Fibonacci number, this program works just fine when my input is not really big:
function nthFibConstantTime(n) {
let phi = (1 + Math.sqrt(5)) / 2;
return Math.round(Math.pow(phi, n) / Math.sqrt(5));
}
nthFibConstantTime(5) // 5
nthFibConstantTime(9) // 34
nthFibConstantTime(788) // 2.1516692574825388e+164
But, with input n
, such as n = 1500, the program return Infinity
and it's not what I expected.
function nthFibConstantTime(n) {
let phi = (1 + Math.sqrt(5)) / 2;
return Math.round(Math.pow(phi, n) / Math.sqrt(5));
}
nthFibConstantTime(1500) // Infinity
Can any body help me to write this program in JavaScript to return the n-th Fiboacci number, in case the output is too large which cannot represent by number in JavaScript, output should return this result with MOD
10^6+7 (n-th % 10^6 + 7).
Constraint input: An integer n (0 < n ≤ 10^15)