Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-23 16:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found