I've been trying to solve this problem:
The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so on until the top which will have a volume of 1^3.
You are given the total volume m of the building. Being given m can you find the number n of cubes you will have to build?
And this is my code
import math
def find_nb(m):
nb = 1
nb_vol = 0
while True:
nb_vol += math.pow(nb, 3)
if (nb_vol == m):
return nb
elif (nb_vol > m):
return -1
nb += 1
Now, when I try to solve for find_nb(2521115597681328384) it returns -1 when it should in fact return 56352. If I change
nb_vol += math.pow(nb, 3)
to
nb_vol += nb ** 3
Everything works correctly. Why?