Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

BrowserUk:

Heh, yep, that would be a good deal slower, all right! Of course, if speed gets to be an issue, you can always skip counting all the iterations, and directly compute the binomial expansion:

use strict; use warnings; use List::Util qw(reduce); my $numTosses = 20; #my $runs = (1<<$numTosses)-1; my @triangle = (0, 1, 0); for (1 .. $numTosses) { my @newTriangle=(0); push @newTriangle, $triangle[$_]+$triangle[$_+1] for 0 .. $#triang +le-1; push @newTriangle, 0; @triangle = @newTriangle; } print <<EOHDR; Tails Count % ----- ---------- ------ EOHDR my $runs = reduce { $a + $b } @triangle; for (my $i = 0; $i < $numTosses+1; $i++) { printf "% 4u % 10u %5.2f\n", $i, $triangle[$i+1], 100*$triangle[$i+1]/$runs; }

It runs a good bit faster:

$ time perl 892293.pl Name "main::a" used only once: possible typo at 892293.pl line 21. Name "main::b" used only once: possible typo at 892293.pl line 21. Tails Count % ----- ---------- ------ 0 1 0.00 1 20 0.00 2 190 0.02 3 1140 0.11 4 4845 0.46 5 15504 1.48 6 38760 3.70 7 77520 7.39 8 125970 12.01 9 167960 16.02 10 184756 17.62 11 167960 16.02 12 125970 12.01 13 77520 7.39 14 38760 3.70 15 15504 1.48 16 4845 0.46 17 1140 0.11 18 190 0.02 19 20 0.00 20 1 0.00 real 0m0.034s user 0m0.024s sys 0m0.012s

Even if you use 32 bits:

$ time perl 892293.pl Name "main::a" used only once: possible typo at 892293.pl line 21. Name "main::b" used only once: possible typo at 892293.pl line 21. Tails Count % ----- ---------- ------ 0 1 0.00 1 32 0.00 2 496 0.00 3 4960 0.00 4 35960 0.00 5 201376 0.00 6 906192 0.02 7 3365856 0.08 8 10518300 0.24 9 28048800 0.65 10 64512240 1.50 11 129024480 3.00 12 225792840 5.26 13 347373600 8.09 14 471435600 10.98 15 565722720 13.17 16 601080390 13.99 17 565722720 13.17 18 471435600 10.98 19 347373600 8.09 20 225792840 5.26 21 129024480 3.00 22 64512240 1.50 23 28048800 0.65 24 10518300 0.24 25 3365856 0.08 26 906192 0.02 27 201376 0.00 28 35960 0.00 29 4960 0.00 30 496 0.00 31 32 0.00 32 1 0.00 real 0m0.034s user 0m0.028s sys 0m0.004s

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Update: When I use bignum;, it prints the PDF for 64 tosses per run in just over a second, but I haven't got it formatted nicely...

use strict; use warnings; use List::Util qw(reduce); use bignum; my $numTosses = 64; #my $runs = (1<<$numTosses)-1; my @triangle = (0, 1, 0); for (1 .. $numTosses) { my @newTriangle=(0); push @newTriangle, $triangle[$_]+$triangle[$_+1] for 0 .. $#triang +le-1; push @newTriangle, 0; @triangle = @newTriangle; } print <<EOHDR; Tails Count % ----- ---------- ------ EOHDR my $runs = reduce { $a + $b } @triangle; for (my $i = 0; $i < $numTosses+1; $i++) { print "$i\t$triangle[$i+1]\t",100*$triangle[$i+1]/$runs, "\n"; }

I guess I'll have to figure out how to make bugnum and printf cooperate better...


In reply to Re^3: Monte Carlo - Coin Toss by roboticus
in thread Monte Carlo - Coin Toss by James_H

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (4)
    As of 2014-09-23 08:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (212 votes), past polls