Scripting competition

Jim Bufalini jim at visitrieve.com
Tue May 12 07:58:52 EDT 2009


Andre Garzia wrote:

> Folks,
> I am coming late for this thread and I do have a cute implementation
> of Fibonacci series!!! it is the smallest I have ever seen but I think
> it's overflowing somewhere, but it should be awarded "fib
> implementation I want printed on a shirt" award.
> 
> function fib k
>     put (1 + sqrt(5)) / 2 into r
>     put 1 - r into t
>     return (r ^ k - t ^ k) / (r - t)
> end fib
> 
> yes that is it, 3 lines... I know it to be running accurate up to
> fib(65) then very accurate until fib(70), then it overflows
> somewhere... silly powers, always exploding my integers.
> 
> and before someone think I am a math genius, that implementation is
> not mine, I came across it sometime ago and ported it to rev.


I'm also late to the party and came across a similar formulation by a
mathematician by the name of Binet:

http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibFormula.html#formu
la

So, here is taking the Binet formula on the above page and similar to what
you did and reduced to one line in Rev:

ON mouseUp pMouseBtnNo
    set the numberformat to "#.0000000000000000"
    ask "Enter the number of iterations" as sheet
    IF it <> empty THEN
        answer fin(it) as sheet
    END IF
END mouseUp
--------------------
FUNCTION fin n
    return ((((1+sqrt(5))/2)^n)-((((1-sqrt(5))/2))^n))/sqrt(5)
END Fin

But, it only works to a precision of 16 digits (both the integer and
fraction portions). After that it's rounded (the rest are zeros). So you
can't get to 211. And, due to rounding, it gets inaccurate much earlier at
71 (or more accurately, starts getting rounded). But, at least it's 0
milliseconds in all cases ;-)

Has anybody been able to get to 211 in Rev? I assume this is not a Rev issue
but a processor issue? Or have I missed something like a formatting?

Aloha from Hawaii,

Jim Bufalini




More information about the use-livecode mailing list