ScriptBasic,
Confused where the array is coming from. Isn't n a simple integer?
Yes, n is a simple integer. I see no reason for it to be a big integer. We are not about to be computing fibo(2000000000) any time soon!
Can you translate Heater's Python code to ScriptBasic?
It's not Python. It's Javascript.
Anyway, if I understand the intention of the ScriptBasic language that is not a translation of the JS.
The original and working JS looks like this:
Code: Select all
function fiboNoMemo (n) {
if (n <= 1) {
return BigInt(n)
}
let k = Math.floor(n / 2)
let a = fibo(k);
let b = fibo(k + 1);
if (isEven(n)) {
return a * ((b * BigInt(2)) - a)
}
return (a * a) + (b * b)
}
Notice those "return" statements there. They exit the function immediately with whatever value is in the expression following.
The last posted translation of that looks like this:
Code: Select all
function fiboNoMemo(n)
if n <= 1 THEN
fiboNoMemo = n
else
k = n \ 2
a = fibo[k]
b = fibo[BI_ADD(k + 1)]
if even(n) then
fiboNoMemo = BI_SUB(BI_MUL(a, BI_MUL(b, 2), a))
end if
end if
fiboNoMemo = BI_ADD(BI_MUL(a, a), BI_MUL(b, b))
end function
Now, if I understand correctly, making an assignment to fiboNoMemo does not exit the function. It merely sets the returned value.
So, whatever this thing dose, it will run through to the end and return a value of "BI_ADD(BI_MUL(a, a), BI_MUL(b, b))". Which is not the intention and no doubt accounts for the undefs.
Perhaps it should look more like:
Code: Select all
function fiboNoMemo(n)
if n <= 1 THEN
fiboNoMemo = n
else
k = n \ 2
a = fiboNoMemo(k)
b = fiboNoMemo(k + 1)
if even(n) then
fiboNoMemo = BI_SUB(BI_MUL(a, BI_MUL(b, 2), a))
else
fiboNoMemo = BI_ADD(BI_MUL(a, a), BI_MUL(b, b))
endif
end if
end function
That's probably not correct syntax but you get the idea.
I suggest to start testing with simple cases:
fiboNoMemo(0)
fiboNoMemo(1)
fiboNoMemo(2)
fiboNoMemo(3)
...
Edit: You should of course be calling fiboNoMemo inside fiboNoMemo, not fibo as you posted. And don't foget the other suggestions I made previously.
Memory in C++ is a leaky abstraction .