We plan to build a house with N floors. Its design blueprint is as follows:
The volume of the bottom floor is N# To the ##3 power, the volume of the penultimate layer is (N-1) to the 3 power, and so on, the volume of the top layer is 1 to the 3 power.
Now comes the problem. Due to various factors, the designer decided to limit the total volume toM.
Is it possible that theM volume is just right to build this house?
Please note, no more, no less. If possible, please return the layer numberN.
findNb (M)
-1.
findNb(1071225) // --> 45层 findNb(91716553919377) // --> -1
M, then start from the top layer and subtract each layer in turn volume until M<=0. If
is equal to0, that is, the house can be built and the number of floors is returned. If
is a negative number, it means that the volumeM is inappropriate and -1 will be returned.
function findNb(m) { var nb = 1; while(m > 0){ m -= Math.pow(nb++,3); } return m == 0 ? nb - 1 : -1; }