Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Re: (Golf) Multiply polynomials

by MeowChow (Vicar)
on May 07, 2001 at 22:05 UTC ( #78588=note: print w/replies, xml ) Need Help??

in reply to Re: (Golf) Multiply polynomials
in thread (Golf) Multiply polynomials

This can be trimmed down to 90 chars:
my($a,$b,@r,$c)=@_;for my$i(0..$#$a){$$c[$i+$_]+=$$a[$i]*$$b[$_]for +0..$#$b}@r?p($c,@r):$c
               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
Re: Re: Re: (Golf) Multiply polynomials
by tilly (Archbishop) on May 08, 2001 at 15:14 UTC
    (Posted at MeowChow's request.)

    My best is 77:

    sub p{ @m=1;for$p(@_){my@t;for$i(0..@m){my$j;$t[$i+$j++]+=$_*$m[$i]for@$p}@m= +@t}[@m] }
    Note that this introduces 0's through a fencepost error, but they don't change which polynomial is represented. I think this is fair, but if you think that is cheating, you can not save that character:
    sub p{ @m=1;for$p(@_){my@t;for$i(0..$#m){my$j;$t[$i+$j++]+=$_*$m[$i]for@$p}@m +=@t}[@m] }
    The trick lies in finding ways to not work through explicit lookups by index, and in finding ways to not access arrays through references. In fact there is not a single lookup by index of an element in an array reference. (It was cheaper to create and manually increment the index variable.)

    BTW note that the statement of the rules anticipated and forbade saving a character by ending with \@m without making @m a private variable.

    Finally at a request from chatter, here is the solution broken out and commented:

    sub p{ @m=1; # Start the product at 1. for$p(@_){ # Loop over the polynomials. my@t; # Create a private temp array. for$i(0..@m){ # Loop over the indexes of @m. my$j; # Create the *other* index var. $t[$i+$j++]+= # Manually increment $j while.. # Adding to the index of @t.. $_*$m[$i] # 2 terms multiplied together.. for@$p # for all the terms in the.. } # other polynomial. @m=@t # Make the temp array our new } # product. [@m] # Return our answer in the } # desired form.

    Never say you are done, 2 more characters:

    sub p{ @m=1;for$p(@_){my@t;for$i(0..@m){$j=$i;$t[$j++]+=$_*$m[$i]for@$p}@m=@t +}[@m] }

    UPDATE 2
    (This is a couple of days later.) Truly never say never, there were 2 more wasted characters to 73:

    sub p{ @m=1;for$p(@_){$i=my@t;for$,(@m){$j=$i++;$t[$j++]+=$_*$,for@$p}@m=@t}[ +@m] }
      D'oh, I see where I was going wrong, I was reading it as [3,2] being 3x+2. Now I see ability to whack zeros with no problem :D

      However, to nitpick, the 75 char solution (update above), isn't strict; you need to add 12 characters to strict-ify it. (my@m, for my $i, for my $p, my$j ), so to compare with the other solutions, at least mine being all strict, you're at 87.

      update-fixed bad square brackets.

      Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
        Nitpicking some more, if you want strict I can give you 84 characters:
        sub p{ my@m=1;for my$p(@_){my@a;for my$i(0..@m){my$j;$a[$i+$j++]+=$_*$m[$i]fo +r@$p}@m=@a}\@m }
        And note that I handle 0 or more polynomials correctly. (The empty product in math is "1".)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://78588]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-06-24 23:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.