Your job is to convert decimals back into the sum of the square roots of integers. The result has to have an accuracy of at least 6 significant decimal digits.
Input:
A number indicating the number of square roots and a decimal indicating the number to approximate.
Example input:
2 3.414213562373095
Output: Integers separated by spaces that, when square rooted and added, are approximately the original decimal accurate to at least 6 significant decimal digits.
Zeros are not allowed in the solution.
If there are multiple solutions, you only have to print one.
Example output (in any order):
4 2
This works because Math.sqrt(4) + Math.sqrt(2) == 3.414213562373095
.
This is code golf. Shortest code (with optional bonus) wins!
There is always going to be a solution but -10 if your program prints "No" when there is no solution with integers. In addition, -10 if your program prints all solutions (separated by newlines or semicolons or whatever) instead of just one.
Test cases:
3 7.923668178593959 --> 6 7 8
2 2.8284271247461903 --> 2 2
5 5.0 --> 1 1 1 1 1
5 13.0 --> 4 4 9 9 9 --> 81 1 1 1 1 --> 36 9 4 1 1 etc. [print any, but print all for the "all solutions bonus"]
And yes, your program has to finish in finite time using finite memory on any reasonable machine. It can't just work "in theory," you have to be able to actually test it.
6 7 8
for the second bonus? \$\endgroup\$