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

Comment on
Binomial Expansion
Download CodeReplies are listed 'Best First'. | |
---|---|

Sierpinskij (Re: Binomial Expansion) by larsen (Parson) on Mar 29, 2001 at 13:48 UTC | |

- (tye)Re: Sierpinskij (Re: Binomial Expansion)
by tye (Cardinal) on Mar 29, 2001 at 21:13 UTC | |

- Re: Sierpinskij (Re: Binomial Expansion)
by rpc (Monk) on Mar 29, 2001 at 23:10 UTC | |

- Re: Sierpinskij (Re: Binomial Expansion)
by oha (Friar) on Dec 19, 2003 at 20:05 UTC | |

Re: Binomial Expansion by ariels (Curate) on Mar 29, 2001 at 14:41 UTC | |

- Re: Re: Binomial Expansion
by japhy (Canon) on Mar 29, 2001 at 21:39 UTC | |

- (crazyinsomniac) Re: (2) Binomial Expansion
by crazyinsomniac (Prior) on Mar 31, 2001 at 04:41 UTC | |

- (tye)Re2: Binomial Expansion
by tye (Cardinal) on Mar 31, 2001 at 12:26 UTC | |

- In theory, theory and practice are the same...
by tilly (Archbishop) on Mar 31, 2001 at 23:32 UTC | |

- Re (tilly) 1: In theory, theory and practice are the same...
by tilly (Archbishop) on Apr 01, 2001 at 05:26 UTC | |

- (tye)Re3: In theory, theory and practice are the same...
by tye (Cardinal) on Apr 01, 2001 at 08:09 UTC | |

Some notes below your chosen depth have not been shown here | |

- Re: Re (tilly) 1: In theory, theory and practice are the same...
by ariels (Curate) on Apr 11, 2001 at 10:55 UTC |

Back to
Craft