16
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.
If there are multiple solutions, does it matter which solution we print?. E.g. for your last test case (5 13.0), this is also a valid solution: 81 1 1 1 1 – Jakube – 2014-11-26T11:11:57.407
And are zeros allowed in the solution? – Jakube – 2014-11-26T11:20:41.567
1Is the input always space-separated? – Sp3000 – 2014-11-26T16:43:00.617
And is entering input via function call allowed? – Jakube – 2014-11-26T16:46:20.723
Also, what about duplicate solutions? For the first example, is our code allowed to print all six permutations of
6 7 8
for the second bonus? – Martin Ender – 2014-11-26T16:49:18.027Sorry, another question: What can the first number be? Can it be
1
or0
? – Sp3000 – 2014-11-26T17:53:46.373@Sp3000 It can be
1
but not0
. – soktinpk – 2014-11-26T19:04:31.323a) Finite time can be a long time. And finite memory might be more than I have available. b) I think you forgot to address some of the other comments. – Martin Ender – 2014-11-26T23:35:54.133
@Sp3000 Yes input is always space separated. – soktinpk – 2014-11-27T00:30:31.283
@Jakube Yes as well. – soktinpk – 2014-11-27T00:31:24.173
@MartinBüttner Sorry, a) I suppose you can print permutations of the same solution if it shortens your code and b) the main point of that rule was that you have to at least test your code once. I didn't want to put a hard number but if your code takes a day you will probably run out of patience although technically it's a valid solution. Essentially, you can't post a solution that says "It takes 2 years to solve the test cases." – soktinpk – 2014-11-27T00:33:44.540
@soktinpk So, my Pyth answer can run the first 2 tests in less than 5 seconds, but it needs about 1 gig of ram to run
5,5.0
and about 1TB of ram to run5,13.0
(which, while very large, are not impossible). I have tested an algorithmically identical solution where the only difference is memory usage. This solves5,13.0
in about 12 hours on my machine. Is this ok? – FryAmTheEggman – 2014-11-27T13:52:10.460Sorry, I can't get the meaning of the 'No' bonus. All solutions are with integers. – edc65 – 2014-11-27T14:21:43.227
@FryAmTheEggman Yes, that's okay because it did terminate in 12 hours right? – soktinpk – 2014-11-27T15:07:20.543