-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
done, source -> https://gist.github.com/mojenmojen/6e9ee60e6be5ffff83…
…9463e75def725e Signed-off-by: AJ <[email protected]>
- Loading branch information
AJ
committed
Aug 17, 2017
1 parent
6e42a7b
commit faaaa3d
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
def find_nb(m): | ||
"""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: | ||
find_nb(1071225) --> 45 | ||
find_nb(91716553919377) --> -1 | ||
Arguments: | ||
m {type[int]} -- volume of whole building. | ||
""" | ||
# create an endless loop | ||
n = 1 | ||
while True: | ||
if m > 0: | ||
# if m, the total volume, is not 0, we will subtract the volume of the current cube from it | ||
# first calculate the volume of the current cube | ||
curr_cube_volume = (n) ** 3 | ||
# subtract the current cube volume from the total volume | ||
m = m - curr_cube_volume | ||
|
||
elif m is 0: | ||
# if m is zero then we've found our n, so return n | ||
return n | ||
|
||
elif m < 0: | ||
# if we've gone below zero there is no such n! | ||
return -1 | ||
|
||
n += 1 |