# Cubic Minefield, Code is too slow?

**URL:** <https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323>\
**Category:** Level Help\
**Created:** [June 15, 2021, 2:23am UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323 "2021-06-15T02:23:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [June 15, 2021, 2:23am UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/1 "2021-06-15T02:23:17Z")

</div>

Here’s my code:

```python
# 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 😳. I’m pretty sure my code is right…

---

<div class="post-metadata">

**Author:** ![lukas31](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lukas31/32/22892_2.png) [@lukas31](https://discourse.codecombat.com/u/lukas31)\
**Post date:** [June 15, 2021, 5:27am UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/2 "2021-06-15T05:27:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [June 15, 2021, 6:10am UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/3 "2021-06-15T06:10:40Z")

</div>

What?

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [June 15, 2021, 6:36am UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/4 "2021-06-15T06:36:01Z")

</div>

@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
```

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [June 15, 2021, 5:40pm UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/5 "2021-06-15T17:40:59Z")

</div>

I got absolutely nothing from what you just told me.

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [June 15, 2021, 7:20pm UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/6 "2021-06-15T19:20:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [June 15, 2021, 7:24pm UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/7 "2021-06-15T19:24:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lukas31](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lukas31/32/22892_2.png) [@lukas31](https://discourse.codecombat.com/u/lukas31)\
**Post date:** [June 15, 2021, 9:51pm UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/8 "2021-06-15T21:51:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [June 15, 2021, 9:52pm UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/9 "2021-06-15T21:52:22Z")

</div>

😶

---

<div class="post-metadata">

**Author:** ![enPointe77](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/enpointe77/32/43632_2.png) [@enPointe77](https://discourse.codecombat.com/u/enPointe77)\
**Post date:** [June 15, 2021, 9:53pm UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/10 "2021-06-15T21:53:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lukas31](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lukas31/32/22892_2.png) [@lukas31](https://discourse.codecombat.com/u/lukas31)\
**Post date:** [June 15, 2021, 9:54pm UTC](https://discourse.codecombat.com/t/cubic-minefield-code-is-too-slow/29323/11 "2021-06-15T21:54:21Z")

</div>

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