It feels like there should be some mathematical formula/pattern that would allow to figure things out without having to loop over all the possible results.

I just found that 1^{3} + 2^{3} +...+ n^{3} = (1/4)n^{2} * (n+1)^{2}

So you would need to reverse that.

m=((1/4)n*(n+1))^{2} sqrt(m)=(1/4)n*(n+1) //given positive n and m 4*sqrt(m)=n*(n+1)

That's as far as I get in my head

n = 1/2 (sqrt(1 - 8 sqrt(m)) - 1)

Wolfram alpha to the rescue

So my solution would be:

const cubes = m => { let n = (Math.sqrt(1 - 8 * Math.sqrt(m)) - 1)/2; return n === Math.floor(n) ? n : 0; }

That's amazing

But it doesn't work. :( Just tested it after posting.

We're a place where coders share, stay up-to-date and grow their careers.

We strive for transparency and don't collect excess data.

## re: Daily Challenge #55 - Building a Pile of Cubes VIEW POST

FULL DISCUSSIONIt feels like there should be some mathematical formula/pattern that would allow to figure things out without having to loop over all the possible results.

I just found that 1

^{3}+ 2^{3}+...+ n^{3}= (1/4)n^{2}* (n+1)^{2}So you would need to reverse that.

m=((1/4)n*(n+1))

^{2}sqrt(m)=(1/4)n*(n+1) //given positive n and m

4*sqrt(m)=n*(n+1)

That's as far as I get in my head

n = 1/2 (sqrt(1 - 8 sqrt(m)) - 1)

Wolfram alpha to the rescue

So my solution would be:

That's amazing

But it doesn't work. :( Just tested it after posting.