Perl Monk, Perl Meditation PerlMonks

### Comment on

 Need Help??
(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.

UPDATE
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]
}

In reply to Re: Re: Re: (Golf) Multiply polynomials by tilly
in thread (Golf) Multiply polynomials by tilly

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!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• 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
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2017-06-23 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (539 votes). Check out past polls.