Cubic Minefield, Code is too slow?

Here’s my code:

# Walk through the minefield

# This function returns the number multiplied by the times
def mult(number, times):
    total = 0
    while times > 0:
        total += number
        times -= 1
    return total

# This function returns the number to the exponent power.
def power(number, exponent):
    total = 1
    # Complete the function.
    while exponent > 1:
        total += number
        exponent -= 2
    return total

# Don't change the following code
# You can find coefficients for the equation on the tower
tower = hero.findFriends()[0]
a = tower.a
b = tower.b
c = tower.c
d = tower.d
x = hero.pos.x

while True:
    # To find the path use a cubic equation
    y = a * power(x, 3) + b * power(x, 2) + c * power(x, 1) + d * power(x, 0)
    hero.moveXY(x, y)
    x = x + 5

But my guy walks down, and I’m pretty sure my greater and lesser signs are correct. It says I ran out of time. And when it says that, one of the ducks says that either the code is too slow, or something else which I can’t remember :flushed:. I’m pretty sure my code is right…

Um it doesn’t look like you run the functions…

What?

@lukas31 you’re right that the mult function doesn’t get used, but that’s how the level is written. The power function is used four times.

@enPointe77 Your power function isn’t right. Have a think about how it’s different to the mult function.

If we use the mult function, and try to find mult(4,5), then going around the loop 5 times calculates 4+4+4+4+4, which outputs 20.

We are trying to write the power function, so if we try to find power(4,5) then we want it to go around the loop 5 times and calculate 4 x 4 x 4 x 4 x 4 (which incidentally will output 1024).

The power function will be really similar to the mult function - you’ve got a small difference between them, but it’s the wrong one. Have another look.

Post again if you need more help.

Jenny

I got absolutely nothing from what you just told me.

Hmm, well it would help if the html hadn’t turned my asterisks into italics. The fourth paragraph should be:

“We are trying to write the power function, so if we try to find power(4,5) then we want it to go around the loop 5 times and calculate 4 x 4 x 4 x 4 x 4 (which incidentally will output 1024).”

If that still makes no sense, maybe go back a stage. Are you familiar with what a power is (eg what it means to say 10 to the power of 4)?

Jenny

No, I’m not familiar with what a power is. And it still doesn’t make sense. Maybe I should wait awhile? This is an extra level after all.

A power is a math thing.
If you know what a square of a number is, like square of 2 is 4, thats 2 power of 2, since you are multiplying 2 by itself once. 3 power of 2 will be 9, since 3 times 3 is 9. You can go to higher powers, such as 2 power of 3 is 8, since 2 X 2 X 2 is 8. For 5 power 6, you just mulptliy 5 times itself 6 times, so the number will be 5x5x5x5x5x5

:no_mouth:

That went completely over my head. I don’t even know what a square of a number is. I think I’ll wait on this level for awhile. Thanks for trying though.

1 Like

No problem. An easier option is search up math antics powers, and it will have an easier explanation

1 Like