물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Build a pile of Cubes

BEstyle 2022. 9. 26. 12:46

DESCRIPTION:

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 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?

The parameter of the function findNb (find_nb, find-nb, findNb, ...) will be an integer m and you have to return the integer n such as n^3 + (n-1)^3 + ... + 1^3 = m if such a n exists or -1 if there is no such n.

Examples:

findNb(1071225) --> 45

findNb(91716553919377) --> -1


 

def find_nb(m):
    target=0
    i=1
    while True:
        target+=i**3
        if target>=m:
            break
        i+=1
    return i if target==m else -1

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Take a Ten Minutes Walk  (0) 2022.09.26
Detect Pangram  (0) 2022.09.26
Two Sum  (0) 2022.09.23
Odd or Even?  (0) 2022.09.23
Highest Scoring Word  (1) 2022.09.23