[Question]
  [
# 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](/questions/tagged/code-golf "show questions tagged '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 : 
![](https://upload.wikimedia.org/wikipedia/commons/thumb/2/26/Nicomachus_theorem_3D.svg/1024px-Nicomachus_theorem_3D.svg.png)
Isn't that nice. 
Link to orignial : [Link](https://math.stackexchange.com/a/61483/585002)
  
[Answer]
# JavaScript (ES7), 31 bytes
A direct formula. Returns `0` if there's no solution.
```
v=>(r=(1+8*v**.5)**.5)%1?0:r>>1
```
[Try it online!](https://tio.run/##ZYrLCsIwEADv/kchm2LZzSaaFRK/pfQhSmkklfx@Kr2Jh5nLzKsv/Tbk5/tzXtM41TnUEqLKQVHrddG6c3CooTvecoxUh7RuaZm6JT3UrCx5RieeLaIAnH6rYaED/ErQ/A0WHYsQ2SsbugDUHQ "JavaScript (Node.js) – 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](https://oeis.org/A000537). This formula can easily be [proved](https://math.stackexchange.com/a/1080587) by induction. [Here](https://math.stackexchange.com/a/61483) 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 =>                    // 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 >> 1              //   return floor(r / 2)
```
---
# Recursive version, ~~36~~ 35 bytes
Returns `NaN` if there's no solution.
```
f=(v,k=1)=>v>0?1+f(v-k**3,k+1):0/!v
```
[Try it online!](https://tio.run/##ZctNDoMgEEDhfW/hDvypMwxUMUHPYqw0LUZMbeb61LVuv5f3GXncp@97@1VrfM4peSe4DA6l67mHAQsvuAp5TmUoUHZQZ5ymuO5xme9LfAkvlDJS3s72uJjGlsDYljSAvVYwZC2ibkjhMac/ "JavaScript (Node.js) – Try It Online")
### Commented
```
f = (v,                   // v = input
        k = 1) =>         // k = current value to cube
  v > 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
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 6 bytes
```
ÝÝOnIk
```
[Try it online!](https://tio.run/##yy9OTMpM/f//8NzDc/3zPLP//zcxtDA2MLW0MDYxMLAEAA "05AB1E – Try It Online")
Port of Jonathan's Jelly answer. Take the cumulative sum of **[0 ... n]**, square each and find the index of **V**.
---
### [05AB1E](https://github.com/Adriandmen/05AB1E), 7 bytes
```
ÝÝ3mOIk
```
[Try it online!](https://tio.run/##yy9OTMpM/f//8NzDc41z/T2z//83MbQwNjC1tDA2MTCwBAA "05AB1E – Try It Online")
### How it works
```
ÝÝ3mOIk – Full program.
ÝÝ      – Yield [[0], [0, 1], [0, 1, 2], ... [0, 1, 2, ... V]].
  3mO   – Raise to the 3rd power.
     Ik – And find the index of the input therein. Outputs -1 if not found.
```
8-byte alternative: `ÝÝÅΔ3mOQ`.
[Answer]
# [R](https://www.r-project.org/), ~~42~~ 40 bytes
*-2 bytes thanks to Giuseppe*
```
function(v,n=((1+8*v^.5)^.5-1)/2)n*!n%%1
```
[Try it online!](https://tio.run/##Hci7CoAwDEDR3b9wEBKfSdNqM/grLkLBJYKov18fw7nDPXKac7psPbfd4G5tBuAm1vfSB3x1jINDq0urKs4JPEehoFE8kWKRwInyj94oue95CqLK7CdxPGJ@AA)
Port of [Arnauld's JavaScript answer](https://codegolf.stackexchange.com/a/171220/76266). Also returns 0 if there's no solution.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~5~~ 4 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
RÄ²i
```
A monadic link, yields `0` if not possible.
**[Try it online!](https://tio.run/##y0rNyan8/z/ocMuhTZn///83NLI0AwA "Jelly – Try It Online")** way too inefficient for the test cases! (O(V) space :p)
[Here](https://tio.run/##y0rNyan8/1/L@MiGQ0uCDrcc2pT5//9/E0MLYwNTSwtjEwMDSwA) is an 8-byte version that performs a cube-root of V first to make it O(V^(1/3)) instead. Using that 8-byte version [here is a test-suite](https://tio.run/##y0rNyan8/1/L@MiGQ0uCDrcc2pT5/@iew@2Pmta4//8frWBiaGFsYGppYWxiYGDJpaNgZGxpCMYGQMLSwAgoZGJgamxpaWhoYm5sZGjGFQsA)
### How?
$$\sum\_{i=1}^{i=n}i^3=\left(\sum\_{i=1}^{i=n}i\right)^2$$
```
RÄ²i - Link: integer, V
R    - range of v -> [1,2,3,...,V]
 Ä   - cumulative sums -> [1,3,6,...,(1+2+3+...+V)]
  ²  - square -> [1,9,36,...,(1+2+3++...+V)²] ( =[1³,1³+2³,1³+2³+3³,...,(1³+2³+3³+...+V³)] )
   i - first 1-based index of v? (0 if not found)
```
[Answer]
# [Elixir](https://elixir-lang.org/), 53 bytes
```
&Enum.find_index 0..&1,fn n->&1*4==n*n*(n+1)*(n+1)end
```
[Try it online!](https://tio.run/##S83JrMgs@p9m@1/NNa80Vy8tMy8lHohTKxQM9PTUDHXS8hTydO3UDLVMbG3ztPK0NPK0DTUhZGpeyn9Pf73MvOKC1OQShTQ9DRNDC2MDU0sLYxMDA0tNLjRJA1NjS0tDQxNzYyNDM83/AA "Elixir – Try It Online")
Port of Jonathan's Jelly answer.
---
# [Elixir](https://elixir-lang.org/), 74 bytes
```
fn v->Enum.find_index 0..v,&v==Enum.sum Enum.map(0..&1,fn u->u*u*u end)end
```
[Try it online!](https://tio.run/##S83JrMgs@p9m@z8tT6FM1841rzRXLy0zLyUeiFMrFAz09Mp01MpsbcESxaW5CmBGbmKBBlBKzVAHqK1U165UCwgVUvNSNIH4v6e/XmZecUFqcolCmp6GiaGFsYGppYWxiYGBpSYXmqSBqbGlpaGhibmxkaGZ5n8A "Elixir – Try It Online")
Definitely sub-optimal. But I am just an Elixir newbie! :) Returns `nil` for "invalid" values of `V`.
[Answer]
# Japt, 7 bytes
```
o³å+ bU
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.6&code=b7PlKyBiVQ==&input=MTI5Ng==)
---
## Explanation
```
            :Implicit input of integer U
o           :Range [0,U)
 ³          :Cube each
  å+        :Cumulatively reduce by addition
     bU     :0-based index of U
```
---
## Alternative
```
Çõ³xÃbU
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.6&code=x/WzeMNiVQ==&input=MTI5Ng==)
[Answer]
# [Cubix](https://github.com/ETHproductions/cubix), 27 bytes (or volume 27?)
Seems like the right place for this language.
```
[[email protected]](/cdn-cgi/l/email-protection)<s)s;;q\.>s-.?/
```
[Try it online!](https://tio.run/##Sy5Nyqz4/9/TQc/QP9zYoCC8IMCmWLPY2rowRs@uWFfPXv//fwA "Cubix – Try It Online")
This wraps onto a 3x3x3 cube as follows
```
      I @ .
      1 O W
      3 0 p
W p P < s ) s ; ; q \ .
> s - . ? / . . . . . .
. . . . . . . . . . . .
      . . .
      . . .
      . . .
```
[Watch it run](https://ethproductions.github.io/cubix/?code=ICAgICAgSSBAIC4KICAgICAgMSBPIFcKICAgICAgMyAwIHAKVyBwIFAgPCBzICkgcyA7IDsgcSBcIC4KPiBzIC0gLiA/IC8gLiAuIC4gLiAuIC4KLiAuIC4gLiAuIC4gLiAuIC4gLiAuIC4KICAgICAgLiAuIC4KICAgICAgLiAuIC4KICAgICAgLiAuIC4K&input=MTI5Ng==&speed=20)
It essential brute forces by taking increasing cubes away from the input. If it results in zero, output *`n`* otherwise if there is a negative result, print 0 and exit.
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), ~~30~~ ~~29~~ 26 bytes
*-4 bytes thanks to Jo King*
```
{first :k,.sqrt,[\+] ^1e4}
```
[Try it online!](https://tio.run/##HcRNCsIwEAbQq3wLkYqlzGSmsWOrF/EPFw2IFjXpphTPHsXFe68@PnweJiwDdnkOt5hGbO9lld5xLA/H9Qln7vWT03VCKBaXFcIzolNuhGprRIkMToz/6JeRg1ItZsy6EcceDIP4fZu/ "Perl 6 – Try It Online")
Brute-force solution for n < 10000. Uses the equation from Jonathan Allan's answer. ~~37~~ 36 bytes solution for larger n (*-1 byte thanks to Jo King*):
```
{!.[*-1]&&$_-2}o{{$_,*-$++³...1>*}}
```
[Try it online!](https://tio.run/##HcRBCsIwEADAr6wQgqYm7GbT2EXtR0SCB3OqVOqphLzKH/ixKB5mnvdliu2xgs5wbmXjLsbSVWuVrK9zKSrtjVVd93k752g0tbbXbYW8VWkHeV7gFGhg7GXggCjgWegPfwl6CNizCFE4sKcIBAIcx2P7Ag "Perl 6 – Try It Online")
Returns `False` if there's no solution.
### Explanation
```
               o  # Combination of two anonymous Blocks
                {                 }  # 1st Block
                 {               }   # Reset anonymous state variable $
                  $_,*-$++³...1>*    # Sequence n,n,n-1³,n-1³-2³,... while positive
{             }  # 2nd Block
 !.[*-1]&&       # Return False if last element is non-zero
          $_-2   # Return length of sequence minus two otherwise
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 28 bytes
```
a=>a**.5%1?0:(2*a**.5)**.5|0
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/Z9m@z/R1i5RS0vPVNXQ3sBKw0gLzNEEETUG//8DAA "JavaScript (Node.js) – Try It Online")
I know it's my own question and all, but I had a better answer (for this lang) then is present, so I posted. Hope it's ok
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 18 bytes
```
{o×⍵≥o←⍵⍳⍨+\3*⍨⍳⍵}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24Tq/MPTH/VufdS5NB/IA7F6Nz/qXaEdY6wFpMCcrbVApQqGXGkKRkamQNLQzMjUDMwysbCwMDUCyZiamQMA "APL (Dyalog Unicode) – Try It Online")
[Answer]
## Matlab, 27 bytes
```
@(v)find(cumsum(1:v).^2==v)
```
Returns the `n` if exists or an empty matrix if not.
**How it works**
```
            1:v            % Creates a 1xV matrix with values [1..V]
     cumsum(   )           % Cumulative sum
                .^2        % Power of 2 for each matrix element
                   ==v     % Returns a 1xV matrix with ones where equal to V
find(                 )    % Returns a base-1 index of the first non-zero element
```
[Try it Online!](https://tio.run/##HYo5CoAwEAB7X7LbhD0SdYWALxFEDViohZrvx6OYKYY5pmvMS0nROVd6yJjWfYbp3s57A@4yukFizFgSiASsEnhulYK16onsC6LGP/TKSP6Jgpox@0aFaywP)
**Note** It fails for large `v` due to memory limitations.
[Answer]
# [Python 3](https://docs.python.org/3/), 60 bytes
```
lambda V:[*[(n*-~n/2)**2for n in range(V+1)],V].index(V)%-~V
```
[Try it online!](https://tio.run/##K6gsycjPM/6fZhvzPycxNyklUSHMKlorWiNPS7cuT99IU0vLKC2/SCFPITNPoSgxLz1VI0zbUDNWJyxWLzMvJbVCI0xTVbcu7H9BUWZeiUaaRmpZYo5GZl5BaYmGJhD8NzYDAA "Python 3 – Try It Online")
-6 thanks to [Mr. Xcoder](https://codegolf.stackexchange.com/users/59487/mr-xcoder).
If we can throw an error in case there's no \$n\$ for a particular \$V\$, we can get this down to 51 bytes:
```
lambda V:[(n*-~n/2)**2for n in range(V+1)].index(V)
```
[Try it online!](https://tio.run/##DcHBDkAgHAfgV@n4L8OwOdi8Rhccsoo2flqLcfHq8X3@ieuBJtl@TJvaZ62Y7AaCyF@UNReitkdgYA4sKCyGZFbxqXDQ5ibJkw8OkSyZS23k4M9I/Jea9gM "Python 3 – Try It Online")
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 33 bytes
```
{$!+>1 if ($!=sqrt 1+8*.sqrt)%%1}
```
[Try it online!](https://tio.run/##HcbRCoJAEEbhV/mFVTQhZnbWbYfUV4kuWgiKau1GxGff0ovzcd639PD5OaOKGPJiinZk3CNqUwzTJ33BbTgct2vKktc8XWfE2lwaxFdC7zgIdRrEESmsKO/RHyULR52oMruTWPZgKMSP5/wD "Perl 6 – Try It Online")
This uses [Arnauld's method](https://codegolf.stackexchange.com/a/171220/76162). Returns an [Empty](https://docs.perl6.org/syntax/Empty) object if the number is not valid.
[Answer]
# [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 19 bytes
```
4*dvvdddk*+d*-0r^K*
```
Input and output is from the stack, returns 0 if no solution.
[Try it online!](https://tio.run/##S0n@b2JoYWxgamlhbGJgYPnfRCulrCwlJSVbSztFS9egKM5b63/BfwA "dc – Try It Online")
# Explanation
If there's a solution n, the input is `((n^2+n)^2)/4`. So we'll calculate a trial solution as `n=sqrt(sqrt(4*input))`, using dc's default 0 decimal place precision for square roots, then compare `(n^2+n)^2` to `4*input` to see if it's actually a solution.
```
4*dvv         Calculate a trial solution n (making a copy of 4*input for later use)
dddk          Store the trial solution in the precision and make a couple copies of it
*+d*          Calculate (n^2+n)^2
-             Subtract from our saved copy of 4*input - now we have 0 iff n is a solution
0r^           Raise 0 to that power - we now have 1 if n is a solution, 0 if not
K*            Multiply by our saved trial solution
```
The penultimate line relies on the non-obvious fact that to dc, `0^x=0` for all nonzero `x` (even negative `x`!) but `0^0=1`.
[Answer]
# [Python 3](https://docs.python.org/3/), ~~53~~ 48 bytes
```
f=lambda V,n=1:V>0and f(V-n**3,n+1)or(not V)*n-1
```
[Try it online!](https://tio.run/##JY1BCsIwEEXX9hSzzNRUGlsEC/UY2YiLaBsdaCYlSRc9fTT44K8en7fu6eO5y@RWHxLEPVa/neKcwvzaQiTPCzlKQrUFzHZcjHtOBrTkUQ361hqewArdcF13ko8KfRDsE2isuVHZ@gAExHDvLhKUhKuEs4S4OVEeUDwXHwy/538HUULfP4bqsAbiJKwgxPwF "Python 3 – Try It Online")
-3 bytes from [Jo King](https://codegolf.stackexchange.com/questions/171219/how-many-cubes-can-be-built/171272?noredirect=1#comment413435_171272)
Returns `-1` for no answer.
Only works up to `n=997` with the default recursion limits.
Repeatedly takes bigger and bigger cubes from the volume until it arrives at zero (success, return number of cubes removed), or a negative number (no answer).
Explanation:
```
f=lambda V,n=1: # f is a recursive lambda taking the volume and the cube size (defaulting to 1)
               V>0and               # if the volume is positive
                      f(V-n**3,n+1) # then we are not to the right cube size yet, try again with n+1, removing the volume of the nth cube
                                   or # if V is not positive
                                     (not V)*n-1
                         # case V == 0:
                         # (not V)*n == n; return n-1, the number of cubes
                         # case V < 0:
                         # (not V)*n == 0; return -1, no answer
```
[Answer]
# [gvm](https://github.com/Zirias/gvm) (commit [2612106](https://github.com/Zirias/gvm/commit/26121066d5955d1b1e11ee8e34715f8ac5d60fc2)) bytecode, 70 59 bytes
(-11 bytes by multiplying in a loop instead of writing the code for multiplying twice)
Hexdump:
```
> hexdump -C cubes.bin
00000000  e1 0a 00 10 00 1a 03 d3  8a 00 f6 2a fe 25 c8 d3  |...........*.%..|
00000010  20 02 2a 04 0a 01 1a 02  00 00 20 08 4a 01 fc 03  | .*....... .J...|
00000020  d1 6a 02 52 02 cb f8 f4  82 04 f4 e8 d1 6a 03 0a  |.j.R.........j..|
00000030  03 fc d5 a8 ff c0 1a 00  a2 00 c0                 |...........|
0000003b
```
Test runs:
```
> echo 0 | ./gvm cubes.bin
0
> echo 1 | ./gvm cubes.bin
1
> echo 2 | ./gvm cubes.bin
-1
> echo 8 | ./gvm cubes.bin
-1
> echo 9 | ./gvm cubes.bin
2
> echo 224 | ./gvm cubes.bin
-1
> echo 225 | ./gvm cubes.bin
5
```
Not really a low score, just using this nice question for testing `gvm` here ;) The commit is older than the question of course. **Note** this is an 8bit virtual machine, so using some code handling only the natural unsigned number range `0-255`, the test cases given in the question won't work.
Manually assembled from this:
```
0100  e1         rud                     ; read unsigned decimal
0101  0a 00      sta     $00             ; store to $00 (target sum to reach)
0103  10 00      ldx     #$00            ; start searching with n = #0
0105  1a 03      stx     $03             ; store to $03 (current cube sum)
0107  d3         txa                     ; X to A
                   loop:
0108  8a 00      cmp     $00             ; compare with target sum
010a  f6 2a      beq     result          ; equal -> print result
010c  fe 25      bcs     error           ; larger -> no solution, print -1
010e  c8         inx                     ; increment n
010f  d3         txa                     ; as first factor for power
0110  20 02      ldy     #$02            ; multiply #02 times for ...
0112  2a 04      sty     $04             ; ... power (count in $04)
                   ploop:
0114  0a 01      sta     $01             ; store first factor to $01 ...
0116  1a 02      stx     $02             ; ... and second to $02 for multiplying
0118  00 00      lda     #$00            ; init product to #0
011a  20 08      ldy     #$08            ; loop over 8 bits
                   mloop1:
011c  4a 01      lsr     $01             ; shift right first factor
011e  fc 03      bcc     noadd1          ; shifted bit 0 -> skip adding
0120  d1         clc                     ; 
0121  6a 02      adc     $02             ; add second factor to product
                   noadd1:
0123  52 02      asl     $02             ; shift left second factor
0125  cb         dey                     ; next bit
0126  f8 f4      bpl     mloop1          ; more bits -> repeat
0128  82 04      dec     $04             ; dec "multiply counter" for power
012a  f4 e8      bne     ploop           ; not 0 yet -> multiply again
012c  d1         clc
012d  6a 03      adc     $03             ; add power to ...
012f  0a 03      sta     $03             ; ... current cube sum
0131  fc d5      bcc     loop            ; repeat unless adding overflowed
                   error:
0133  a8 ff      wsd     #$ff            ; write signed #$ff (-1)
0135  c0         hlt                     ; 
                   result:
0136  1a 00      stx     $00             ; store current n to $00
0138  a2 00      wud     $00             ; write $00 as unsigned decimal
013a  c0         hlt
```
*edit*: I just [fixed a bug](https://github.com/Zirias/gvm/commit/e530d929691ade45468ea56846b6c4b27d354b7c) in `gvm`; without this fix, `gvm` tried to read binary programs in *text mode*, which might break (code above doesn't contain any `0xd` bytes so won't break on windows without this fix).
[Answer]
# [K (oK)](https://github.com/JohnEarnest/ok), 21 bytes
```
{(,_r%2)@1!r:%1+8*%x}
```
[Try it online!](https://tio.run/##y9bNz/7/P82qWkMnvkjVSNPBULHIStVQ20JLtaL2f5q6iaGFsYGppYWxiYGBpYKRsaUhGBsACUsDIwUTA1NjS0tDQxNzYyNDs/8A "K (oK) – Try It Online")
Port of [Arnauld's JS answer](https://codegolf.stackexchange.com/a/171220/76266).
### How:
```
{(,_r%2)@1!r:%1+8*%x} # Main function, argument x
             %1+8*%x  # sqrt(1+(8*(sqrt(x)))
           r:         # Assign to r
         1!           # r modulo 1
        @             # index the list:
 (,_r%2)              # enlist (,) the floor (_) of r modulo 2.
```
the function will return `(_r%2)` iff `1!r == 0`, else it returns null (`0N`). That is due to the single element in the list having index 0, and trying to index that list with any number other than 0 will return null.
]