Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
    0: #!perl -w
    1: 
    2: # BinomialExpansion.pl
    3: 
    4: # usage perl BinomialExpansion.pl n, where n is an integer > 0 
    5: 
    6: # ** the memory goes crazy after the 170th power
    7: # the expansion of (x+y)^170 peaks at 9.14484184513157e+049 * x^85 * y^85
    8: # so you can imagine what happens if you put int n>170
    9: 
    10: my $n = ($_=shift) > 0 ? int $_ :
    11:         die "'usage perl BinomialExpansion.pl 9, where n > 0'";
    12: 
    13: print _titled_hr(' Binomial Expansion For (x+y)^',$n,' ');
    14: 
    15: for my $j (0 .. $n)
    16: {
    17:     my $coefficient = nCr($n,$j);
    18:     my $nj=$n-$j;
    19:     print $coefficient;
    20:     print $_ = ($nj!=0)?( ($nj>1)?(' * x^'.$nj):(' * x') ):'';
    21:     print $_ = ($j!=0)?( ($j==1)?(' * y'):(' * y^'.$j) ):'';
    22:     print $_ = ($j!=$n)?(" +\n"):("\n");
    23: }
    24: 
    25: print ' 'x 25,' = (x + y)^',$n, "\n"x 3;
    26: 
    27: # returns n!/r!(n-r)!
    28: sub nCr
    29: {
    30:     my $n=shift;
    31:     my $r=shift;
    32: 
    33:     return int nFactorial($n) / int nFactorial($r) * int nFactorial($n-$r);
    34: }
    35: 
    36: # like the name says, n!
    37: sub nFactorial
    38: {
    39:     my $n=shift;
    40:     my $product = 1;
    41: 
    42:     while($n>0)
    43:     {
    44:         $product *= $n--;
    45:     }
    46: 
    47:     return  $product;
    48: }
    49: 
    50: # neat little titled hr, that does a < 80 chars since int rounds down
    51: # i really, really, like it
    52: sub _titled_hr
    53: {
    54:     my $string = join('', @_);
    55:     my $oy = int (80 -(length $string) )/ 2;
    56:     return "\n","-" x $oy, $string, "-" x $oy,"\n";
    57: }
    58: 
    59: __END__
    60: # some random things i say
    61: #1
    62: "--. .-. . . - --..   -.-- .----. .- .-.. .-.."
    63: 
    64: #2
    65: "...- .-. --- --- -- --..--   ...- .-. --- --- -- .----."
    66: 
    67: #3
    68: --- ..-.   .- .-.. .-..   - .... .   - .... .. -. --. ...   ..   .-.. --- ... -
    69: --..--
    70:   ..   -- .. ... ...   -- -.--   -- .. -. -..   - .... .   -- --- ... - .-.-.-
    71:     -....- -....-   --- --.. --.. .. .   --- ... -... --- ..- .-. -. .
    72: 
    73: # a lil sample from my machine
    74: 
    75: F:\>perl BinomialExpansion.pl 9
    76: 1 * x^9 +
    77: 9 * x^8 * y +
    78: 36 * x^7 * y^2 +
    79: 84 * x^6 * y^3 +
    80: 126 * x^5 * y^4 +
    81: 126 * x^4 * y^5 +
    82: 84 * x^3 * y^6 +
    83: 36 * x^2 * y^7 +
    84: 9 * x * y^8 +
    85: 1 *  * y^9
    86:                  = (x + y)^9
    87: F:\>
    88: 
    89: # Notice a pattern? Fleet attack!
    90: # >
    91: #   >
    92: #     >
    93: #     >
    94: #       >
    95: #       >
    96: #     >
    97: #     >
    98: #   >
    99: # >

In reply to Binomial Expansion by crazyinsomniac

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 scrutinizing the Monastery: (7)
    As of 2014-12-27 17:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (177 votes), past polls