-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#6_kyu_Build_a_pile_of_Cubes.py
51 lines (44 loc) · 1.2 KB
/
#6_kyu_Build_a_pile_of_Cubes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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) --> -1mov rdi, 1071225
call find_nb ; rax <-- 45
mov rdi, 91716553919377
call find_nb ; rax <-- -1
"""
My codes:
def find_nb(m):
cubes = 0
i = 1
while cubes < m:
cubes += i**3
i += 1
return i-1 if cubes == m else -1
Others codes:
def find_nb(m):
n = 1
volume = 0
while volume < m:
volume += n**3
if volume == m:
return n
n += 1
return -1
def find_nb(m):
n = 1
volume = 0
while volume < m:
volume += n**3
if volume == m : return n
n += 1
return -1