in reply to Re: Fibonacci Numbers
in thread Fibonacci Numbers

Not that it matters much to me, but for the 71st number I get a round-off error using Binet's formula.

sub fib {int(((((1+sqrt(5))/2)**$_[0])/sqrt(5))+.5)} sub fib2 { my ($n) = @_; my ($x, $y) = (1, 1); ($x, $y) = ($y, $x + $y) for 3 .. $n; return $y; } for (my $n = 1; 1; $n++) { my ($x, $y) = (fib($n), fib2($n)); if ($x != $y) { print "$n: $x $y"; last; } } __END__ 71: 308061521170130 308061521170129

Regards,
ihb

See perltoc if you don't know which perldoc to read!

Replies are listed 'Best First'.
Re^3: Fibonacci Numbers
by jweed (Chaplain) on Feb 15, 2005 at 04:53 UTC
    Try Math::BigFloat's rounding handling instead of the int(foo+.5) trick, and things should work out.



    Code is (almost) always untested.
    http://www.justicepoetic.net/