I thought this code looked a bit odd, so I took the trouble to install Math::Big and play with it.

1. I think you have an "off by one" problem. $fibonacci[$n-1] does not appear to be correct.
2. The need for the fib subroutine is unclear to me.
3. This fibonacci subroutine in the Math::Big library appears to be so fast, that the need for Memoize is also unclear to me...this thing barfs out fibonacci(1000) with incredible speed!

Below is my testing code:

#!/usr/bin/perl use strict; use warnings; use Math::Big qw/fibonacci/; my $n=6; print scalar(fibonacci($n)), "\n"; print "".fibonacci($n), "\n"; #another way to force scalar context my @fibs = fibonacci($n); print "@fibs\n"; print $fibs[-1],"\n"; #NOT $n-1, [$n] same as [-1] here. foreach (1000,2000,3000) { #print "".fibonacci($_), "\n\n"; #prints HUGE numbers not shown below } __END__ prints: .....testing the fibonacci of 6, which is indeed 8.... 8 8 0 1 1 2 3 5 8 8
Anyway, I figure that we are getting pretty "far into the outfield" on this one. Yes, there is a library function for generating a fibonacci number. Yes, it is very fast. Yes, it can calculate these numbers to an incredible number of digits. No, it does not use a recursive algorithm. The library function likes to start the sequence at 0 instead of 1, but that is allowed within the definition of the sequence. Of course it is completely possible that I've misunderstood something - wouldn't be the first time...let me know.

In reply to Re^4: fibonacci numbers using subroutine? by Marshall
in thread fibonacci numbers using subroutine? by derpp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.