in reply to (Golf) Multiply polynomials
use strict; my @poly1 = ( 2, 4, 6 ); my @poly2 = ( 3, 1, 0 ); my @poly4 = ( 5, 2, 1, 2 ); my @poly3 = p( \@poly1, \@poly2, \@poly4 ); print (join',',@poly3)."\n"; my @poly5 = p( \@poly4, \@poly1, \@poly2 ); print (join',',@poly5)."\n"; sub p { my$a=shift;my$b=shift;my(@c,$i,$j);for$i(0..$#$a){for$j(0..$#$b){$c[ +$i+$j]+=$$a[$i]*$$b[$j]}};@_?p(\@c,@_):\@c }
Update: Cut down 5 more characters to 105, since you don't need to have $j around in the inner loop:
sub p { my$a=shift;my$b=shift;my(@c,$i);for$i(0..$#$a){for(0..$#$b){$c[$i+$_ +]+=$$a[$i]*$$b[$_]}};@_?p(\@c,@_):\@c }
Update #2 as per tilly's reply, fixed the return problem to add that extra character.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: (Golf) Multiply polynomials
by MeowChow (Vicar) on May 07, 2001 at 22:05 UTC | |
by tilly (Archbishop) on May 08, 2001 at 15:14 UTC | |
by Masem (Monsignor) on May 08, 2001 at 18:14 UTC | |
by tilly (Archbishop) on May 08, 2001 at 18:29 UTC | |
by Masem (Monsignor) on May 08, 2001 at 18:52 UTC | |
| |
Re (tilly) 2: (Golf) Multiply polynomials
by tilly (Archbishop) on May 07, 2001 at 18:57 UTC | |
Re: Re: (Golf) Multiply polynomials
by Masem (Monsignor) on May 07, 2001 at 22:00 UTC | |
by tye (Sage) on May 07, 2001 at 22:09 UTC |