I find out the n th number of the fibonacci sequence by doing this:
function add(a, b) {
while (a.length < b.length) a.unshift(0);
while (a.length > b.length) b.unshift(0);
var carry = 0, sum = []
for (var i = a.length - 1; i >= 0; i--) {
var s = a[i] + b[i] + carry;
if (s >= 10) {
s = s - 10;
carry = 1;
} else {
carry = 0;
}
sum.unshift(s);
}
if (carry)
sum.unshift(carry);
return sum;
}
function fib(n) {
var f1 = [0];
var f2 = [1];
while (n--) {
var f3 = add(f1, f2)
f1 = f2;
f2 = f3;
}
return f1.join("");
}
Say, I want to find the remainder when the 1995th fibonacci number is divided by 8. Doing this,
fib(1995) % 8
returns, however, NaN
. Here's a fiddle with output to console.log
.
So how do I find the remainder of the 1995th fibonacci number divided by 8?