Improper Polynomial Equation Response

(This is a template, feel free to delete it.)

What’s problem you are encountering?

The provided script gives me the answer “-82650”, when the correct answer is “-82640”. I’ve tried removing the line “add (Base Value) to (Quadratic List)”, but that got me “-82600”.

Graphing by johnce | Snap! Build Your Own Blocks (Open code, find “Polynomial equation calculator: Base value: __ to degree: __”, and edit. To test, use coefficients -1, 2, -3, 4, and -5.

I think your bug is that if degree=5 you really need six coefficients, including the constant term. But I’m not sure.

But I’m guessing you’re a recent refugee from Scratch, because this can be done much more easily in Snap!. For one thing, we have an exponentiation operator:
untitled script pic (10)

Next, we can apply that operator to several numbers at once:

And then we can multiply each power of the base by the corresponding coefficient:


(I put a zero in front of your list of coefficients to account for the constant term.)

And we can add those values up:
untitled script pic (15)
(You won’t see a SUM block in the palette, but if you drag the × block over the arrowheads of the + block:


it’ll turn into a SUM block when you let go of the mouse.)

But probably the user of your block would like to have the coefficient of the largest exponent at the left, and the constant term at the right, so I counted the exponents down to zero rather than up from zero:

untitled script pic (16)

If you don’t like to have to use a LIST block to enter the coefficients, you can add a little syntactic sugar by making COEFFICIENTS a variadic numeric input:

untitled script pic (18)

Nah, I just did it that way for convince of indexing. I actually never liked scratch and used it for maybe a week in middle school solely because I had too. It always felt like they dumb’ed things down too much, imo.

On to the other things you mentioned. I never actually knew you could do that with lists! My APCSP class has only just started going over lists with the shopping list lesson, so I’m not super similar with list functionalities still. Same with SUM, didn’t know that, but that’s actual really handy!

The code you gave does work, and much more simply at that. Now I’ll start thinking on how to inc operate that into my graphing calculator to make it actually graph.

Thank you a lot!

Actual, I have a question.

Why “length of coefficients -1 to 0” and then have an appended 0 at the end of your list. Instead, just leave it as “length of coefficients to 0” and for the constant just have a separate input that gets added at the end, and remove the appended 0 at the end of the coefficient list. This still gets you the same right answer, and trims down the complexity a little, no?

I didn’t mean that the code should append a zero, just that I appended a zero to your example to account for the fact that a degree-five polynomial actually has six terms. In actual use, the block should allow for any constant term, not assume zero!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.