It runs in 61 seconds. That's 3 seconds slower than the hfibo.js. As noted most of that time is spent formatting the result for printing. The actual calculation time is less than 10 seconds with 1milfibo being 3 seconds slower.
So we see the recursive solution is faster than the iterative. Which is what we expected to see and contrary to popular opinion here. All this talk of "overheads of recursive function calls etc" is just wrong in many situations.
Quite why script basic is having such difficulty with this would be interesting to know.
Code: Select all
let count = 4784969
let a = BigInt(1)
let b = BigInt(0)
let p = BigInt(0)
let q = BigInt(1)
while (count > 0) {
if ((count % 2) == 0) {
let psq = p * p
let qsq = q * q
let twopq = p * q
twopq = twopq * 2n
p = psq + qsq
q = twopq + qsq
count = count / 2
} else {
let bq = b * q
let aq = a * q
let ap = a * p
let bp = b * p
a = bq + aq
a = a + ap
b = bp + aq
count = count - 1
}
}
console.log(b)
Code: Select all
$ time node 1milfibo.js | tail -c 100
80149736853685001275152076875379936330930391815964864885353407167474856539211500699706378405156269n
real 1m0.885s
user 1m0.734s
sys 0m0.141s