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; }
That's amazing
But it doesn't work. :( Just tested it after posting.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
That's amazing
But it doesn't work. :( Just tested it after posting.