Good morning, all.
This challenge finishes our second week of the Daily Challenge series. It'll be our most difficult challenge yet!
We are grateful to user g964, who posted this challenge and many others on CodeWars.
Given a positive integer
n
, return a strictly increasing sequence of numbers so that the sum of the squares is equal to n².If there are multiple solutions, return the result with the largest possible value:
For example:
decompose(11)
must return[1,2,4,10]
.Note: there are actually two ways to decompose
11², 11² = 121 = 1 + 4 + 16 + 100 = 1² + 2² + 4² + 10²
but you shouldn't return[2,6,9]
, since 9 is smaller than 10.Hint: Very often will
xk
ben-1
.
Good luck and happy coding!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Top comments (9)
I think this works for JavaScript:
JavaScript
And a live demo on CodePen.
Not really inspired lately, my code is getting bigger and messier... and I'm running behind on these challenges :-/
I feel ya! Me tooooo
I got this one started but ran into a tougher example that I haven't worked through yet! Definitely want to get it wrapped up but I'm definitely not doing 1 a day anymore
Perl solution, using recursion:
I added 12 as a test, too, because for 12, 11 is not part of its decomposition.
Here is my Rust version!
I started with an iterative solution, but I was working off a simplifying assumption that turned out to be false once I starting testing larger examples.
After I realized my simplification wasn't going to work I went for a recursive solution. I think it turned out pretty well! I also got to try out my first Macro in Rust so that was cool!
Python solution:
Once try for 50 You should get[1,3,5,8,49],
This is the use full blog here most people have to seen it and save the batter update of euchre free online really thanks for the share me it is the amazing.