I'm designing a new space station for generic super-villain purposes (something something megalaser), but I'm having trouble designing the solar panels.
My genius team of scientists can calculate exactly how many square meters of paneling we need to power the station, but the problem is our solar panels only come in squares!
Thankfully due to a generous supply of duct tape, my minions can stitch together the right squares to make up the surface area needed, but my scientists have gone on strike (something about cyanide not being an ethical coffee sweetener) and my minions are too stupid to figure out what squares they need to connect.
That's where you come in minion loyal golfer. I need some code that will take the target surface area and tell me what size solar panels my minions need to tape together to reach it.
The minions have a tiny budget, so they still have to program their computer by punchcard. Time spent programming is time not spent taping, so make sure your code is as small as possible!
The Challenge
Given a positive integer n
, output the smallest list of square numbers that sums to n
.
A square number is any integer that is the result of multiplying an integer by itself. For example 16
is a square number because 4 x 4 = 16
This is A000290
For example:
For n = 12
, you could achieve the desired size with 4 panels of sizes [9, 1, 1, 1]
(note that this is the correct answer for the Google FooBar variant of this challenge), however this is not the smallest possible list, because you can also achieve the desired size with 3 panels of sizes [4, 4, 4]
For n = 13
, you can achieve the desired size with only 2 panels: [9, 4]
If n
is a square number, the output should be [n]
.
Input
A positive integer n
representing the total desired surface area of the solar panels.
Note that 0
is not positive
Output
The smallest possible list of square numbers that sums to n
, sorted in descending order.
If there are multiple smallest possible lists, you may output whichever list is most convenient.
Testcases
1 -> [1]
2 -> [1,1]
3 -> [1,1,1]
4 -> [4]
7 -> [4,1,1,1]
8 -> [4,4]
9 -> [9]
12 -> [4,4,4]
13 -> [9,4]
18 -> [9,9]
30 -> [25,4,1]
50 -> [49,1] OR [25,25]
60 -> [49,9,1,1] OR [36,16,4,4] OR [25,25,9,1]
70 -> [36,25,9]
95 -> [81,9,4,1] OR [49,36,9,1] OR [36,25,25,9]
300 -> [196,100,4] OR [100,100,100]
1246 -> [841,324,81] OR one of 4 other possible 3-length solutions
12460 -> [12100,324,36] OR one of 6 other possible 3-length solutions
172593 -> [90601,70756,11236] OR one of 18 other possible 3-length solutions
18 -> [9,9]
(not[16,1,1]
). \$\endgroup\$18 -> [9, 9, 0, 0]
? \$\endgroup\$1246 = 34*34 + 9*9 + 3*3
? \$\endgroup\$