DEV Community

Discussion on: Daily Challenge #55 - Building a Pile of Cubes

Collapse
 
dominikfg profile image
Dominik G • Edited

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

Collapse
 
dominikfg profile image
Dominik G • Edited

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

Wolfram alpha to the rescue

Thread Thread
 
dominikfg profile image
Dominik G

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;
}
Thread Thread
 
alvaromontoro profile image
Alvaro Montoro

That's amazing

Thread Thread
 
dominikfg profile image
Dominik G

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