Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

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



  • 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 wandering the Monastery: (2)
As of 2024-04-19 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found