Code Self Study Forum

Puzzle: Build a pile of Cubes [reference]

Build a pile of Cubes” is a coding puzzle that people can be attempted in the following languages: csharp, java, ruby, python, javascript, coffeescript, haskell, clojure, elixir, typescript, cpp, php, crystal, fsharp, c, swift, rust, go, r, shell, ocaml, fortran, julia, nasm, nim, scala, kotlin, and groovy.

  • Difficulty: 6 kyu
  • Stars: 1548
  • Votes: 4016
  • Category: reference
  • Tags: Fundamentals, Optimization
  • Source: codewars


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.


findNb(1071225) --> 45
findNb(91716553919377) --> -1

mov rdi, 1071225
call find_nb            ; rax <-- 45

mov rdi, 91716553919377
call find_nb            ; rax <-- -1

Solve It Here

Click the link below to solve it on Codewars:


This puzzle was posted by a Slackbot via a slash command. If you want to help work on the app, send a message to @Josh.

If you don’t want to see the coding puzzles when you visit the forum, you can go into your settings and mute the puzzles category.