It won't work as well, but it works well enough. My benchmark for 5000 says:
Method 6: 14 wallclock secs (14.31 usr + 0.01 sys = 14.32 CPU) Method 7: 15 wallclock secs (14.28 usr + 0.01 sys = 14.29 CPU) Method 8: 14 wallclock secs (14.25 usr + 0.01 sys = 14.26 CPU)
I got rid of the Math::BigInt call on the list of 1 then ran it a bunch of times, and there seemed to be some fluctuation. Another run gave me:
Method 6: 14 wallclock secs (14.08 usr + 0.02 sys = 14.10 CPU) Method 7: 14 wallclock secs (14.14 usr + 0.01 sys = 14.15 CPU) Method 8: 15 wallclock secs (14.12 usr + 0.01 sys = 14.13 CPU)
Not bad for such a stupid approach, huh?

In truth yours is better by about as much as you lose because you used the overloading interface. If I use the overloading interface, well

sub fact6b{ my ($m, $n) = @_; if ($m < $n) { my $k = int($m/2 + $n/2); return my $x = fact6b($m, $k) * fact6b($k+1,$n); } else { return Math::BigInt->new($m); } }
is very straightforward to anyone who has seen divide-and-conquer before. And recursive is only bad because Perl penalizes function calls horribly.

Of course at this point we are both slow mainly because Perl doesn't implement a good algorithm for big integer multiplication.


In reply to Re: Re^4: Factorial algorithm execution time by Anonymous Monk
in thread Factorial algorithm execution time by gri6507

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.