How many cubes can be built
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