m = 1000000000 '10^9
'using the Binet formula for the Fib number ( ( (1+sqr(5))/2 )^n - ( (1-sqr(5))/2 )^n )/sqr(5)
'the apprximation of which is the nearest integer of ( ((1+sqr(5))/2)^n )/sqr(5)
'we take the Log10 of the last expression log10((1+sqr(5))/2)*n)-log10(sqr(5)
'10^FractionalPart is approximately equal the the fib number, the integer part is the exponent
f
= f
- Fix(f
) 'Fractional partf = 10## ^ f
Print "the first";
Len(s
);
" digits of Fibonacci ";
fn;
" are "; s
fib = k
b = k
x = 1: y = 0
bit_length
= Fix(Log(k
) / Log(2)) 'Log2(n) giges the highest bit set of an integer x
= (xx
+ 2 * x
* y
) Mod m
If b
And 1 Then 'if the least significant bit is 1 then temp = x
y = temp
b = b \ 2 'chop off the least significant bit for the next test
fib = x