How many cubes can be built task

Your task is to build a structure with \$n\$ cubes. The volume of cubes follow the following sequence (bottom -> top) 

\$n^3, (n-1)^3, (n-2)^3,...,1^3\$

input

The total volume of the structure (\$V\$).

output

value of (\$n\$), i.e : The total number of cubes.

\$V = n^3 + (n-1)^3 + .... + 1^3\$ 

notes


Input will always be an integer.
Sometimes it isn't possible to follow the sequence, i.e : \$V\$ doesn't represent a specific value for \$n\$. In that event return -1, or a falsy value of your choosing (consistency is required though).
This is code-golf so shortest answer in bytes for each language wins.
No answer will be marked accepted for the above mentioned reason.  


requests


This is my first challenge on the site so bear with me, and forgive (and tell me about) any mistakes that I made.
Kindly provide a link so your code can be tested.
If you can, kindly write an explanation on how your code works, so others can understand and appreciate your work.


examples

input  : 4183059834009
output : 2022

input  : 2391239120391902
output : -1

input  : 40539911473216
output : 3568




Thanks to @Arnauld for the link to this : 



Isn't that nice. 

Link to orignial : Link
JavaScript (ES7), 31 bytes

A direct formula. Returns 0 if there's no solution.



v=&gt;(r=(1+8*v**.5)**.5)%1?0:r&gt;&gt;1


Try it online!

How?

The sum \$S_n\$ of the first \$n\$ cubes is given by:

$$S_n = \left(\frac{n(n+1)}{2}\right)^2 = \left(\frac{n^2+n}{2}\right)^2$$

(This is A000537. This formula can easily be proved by induction. Here is a nice graphical representation of \$S_5\$.)

Reciprocally, if \$v\$ is the sum of the first \$x\$ cubes, the following equation admits a positive, integer solution:

$$\left(\frac{x^2+x}{2}\right)^2=v$$

Because \$(x^2+x)/2\$ is positive, this leads to:

$$x^2+x-2\sqrt{v}=0$$

Whose positive solution is given by:

$$\Delta=1+8\sqrt{v}\\
x=\frac{-1+\sqrt{\Delta}}{2}
$$

If \$r=\sqrt{\Delta}\$ is an integer, it is guaranteed to be an odd one, because \$\Delta\$ itself is odd. Therefore, the solution can be expressed as:

$$x=\left\lfloor\frac{r}{2}\right\rfloor$$

Commented

v =&gt;                    // v = input
  ( r =                 //
    (1 + 8 * v ** .5)   // delta = 1 + 8.sqrt(v)
    ** .5               // r = sqrt(delta)
  ) % 1 ?               // if r is not an integer:
    0                   //   return 0
  :                     // else:
    r &gt;&gt; 1              //   return floor(r / 2)




Recursive version, 36 35 bytes

Returns NaN if there's no solution.



f=(v,k=1)=&gt;v&gt;0?1+f(v-k**3,k+1):0/!v


Try it online!

Commented

f = (v,                   // v = input
        k = 1) =&gt;         // k = current value to cube
  v &gt; 0 ?                 // if v is still positive:
    1 +                   //   add 1 to the final result
    f(                    //   do a recursive call with:
      v - k ** 3,         //     the current cube subtracted from v
      k + 1               //     the next value to cube
    )                     //   end of recursive call
  :                       // else:
    0 / !v                //   add either 0/1 = 0 if v is zero, or 0/0 = NaN if v is
                          //   non-zero (i.e. negative); NaN will propagate all the
                          //   way to the final output