Okay, I have succumb to my curiosity: Because this turned out to be a good example of our motto (TIMTOWTDI) ... I had to do a benchmark with all the possibilities
use strict; use Benchmark; timethese(1000000, { 'p1' => sub { p1( 50, 5) }, 'p2' => sub { p2( 50, 5) }, 'p3' => sub { p3( 50, 5) }, 'p4' => sub { p4( 50, 5) }, 'p5' => sub { p5( 50, 5) }, 'p6' => sub { p6( 50, 5) }, 'p7' => sub { p7( 50, 5) }, }); #chromatic sub p1 { my $probability = 1; my ($number, $times) = @_; while ($times-- > 0) { $probability *= $number; $number--; } return $probability; } #btrott sub p2 { my($number, $times, $prob) = (@_, 1); $prob *= $number-- while $times--; $prob; } #perlmonkey sub p3 { my $number = shift; my $count = shift || return 1; return $number * p3($number-1,$count-1); } #perlmonkey sub p4 { return $_[1] ? $_[0] * p4($_[0]-1,$_[1]-1) : 1; } #chromatic sub p5 { my ($prob, $start) = (1, shift); $prob *= $start-- foreach (1 .. $_[0]); $prob; } #chromatic sub p6 { my $prob = 1; $prob *= $_ foreach ((($_[0]+1) - $_[1]) .. $_[0]); return $prob; } #chromatic sub p7 { my $prob = 1; return (map { $prob *= $_ } (($_[0] - $_[1] + 1) .. $_[0]))[-1]; }
And the results:
Benchmark: timing 1000000 iterations of p1, p2, p3, p4, p5, p6, p7... p1: 61 wallclock secs (45.42 usr + 0.16 sys = 45.58 CPU) p2: 45 wallclock secs (35.92 usr + 0.11 sys = 36.03 CPU) p3: 127 wallclock secs (102.34 usr + 0.31 sys = 102.65 CPU) p4: 90 wallclock secs (71.84 usr + 0.31 sys = 72.15 CPU) p5: 58 wallclock secs (46.11 usr + 0.14 sys = 46.25 CPU) p6: 55 wallclock secs (43.51 usr + 0.15 sys = 43.66 CPU) p7: 85 wallclock secs (68.11 usr + 0.22 sys = 68.33 CPU)
And the winner is ... btrott with the slimmed down version from chromatic.
And the loser is... me as I suspected, recursion is slow.

In reply to Re: New Jersey Lottery Probability by perlmonkey
in thread New Jersey Lottery Probability by ergowolf

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.