MATHEMATICS
ALGORITHMS
Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of
You are given the total volume m of the building. Can you find the number n of cubes you will have to build?
The parameter of the function (find_nb, find-nb, findNb, ...)
will be an integer m, and you have to return the integer n such as:
Examples:
findNb(1071225) --> 45
findNb(91716553919377) --> -1
const findNb = totalVolume => {
let currentVolumeSum = 0
let numberOfCubes = 0
while (currentVolumeSum < totalVolume) {
numberOfCubes++
currentVolumeSum += Math.pow(numberOfCubes, 3)
}
return currentVolumeSum === totalVolume ? numberOfCubes : -1
}