Dear Monks, I want to calculate the propability to get side 1 at least k times, tossing a coin n times.

The following code works fine with numbers up to about 150 but when I use numbers above, I get the result -1.#IND

Is there a better way to solve this problem?

#!/usr/bin/perl -w $coin_toss=1000; # =n $toss_side1=60; # =k $total_p=0; foreach($toss_side1..$coin_toss) { # calculate factorials $n_fac=fac($coin_toss); $n_minus_k_fac=fac($coin_toss-$_); $k_fac=fac($_); # calculate propability for getting side 1 exactly $_ times $partial_p=($n_fac/($n_minus_k_fac*$k_fac))*(0.5**$_)*(0.5**($coin +_toss-$_)); # add propability to total propapility $total_p=$total_p+$partial_p; } print"Propability to get side 1 at least $toss_side1 times tossing a c +oin $coin_toss times: $total_p\n"; system("pause"); exit; # subroutine for calculating factorials sub fac { $_[0]>1?$_[0]*fac($_[0]-1):1; }

In reply to Calculate propabilities without recursion? Coin toss. by Microcebus

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.