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

FULL DISCUSSION
 

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 13 + 23 +...+ n3 = (1/4)n2 * (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;
}
code of conduct - report abuse