11
2
You are given a list of numbers L = [17, 5, 9, 17, 59, 14]
, a bag of operators O = {+:7, -:3, *:5, /:1}
and a number N = 569
.
Task
Output an equation that uses all numbers in L
on the left-hand side and only the number N
on the right-hand side. If this is not possible, output False. Example solution:
59*(17-5)-9*17+14 = 569
Limitations and Clarification
- You may not concatenate numbers (
[13,37]
may not be used as1337
) - Only natural numbers and zero will appear in
L
. - The order in
L
doesn't matter. - You must use all numbers in
L
. - Only the operators
+
,-
,*
,/
will appear inO
. O
can have more operators than you need, but at least|L|-1
operators- You may use each operator any number of times up to the value in
O
. - All four operations in
O
are the standard mathematical operations; in particular,/
is normal division with exact fractions.
Points
- The less points, the better
- Every character of your code gives you one point
You have to provide an un-golfed version that is easy to read.
Background
A similar question was asked on Stack Overflow. I thought it might be an interesting code-golf challenge.
Computational Complexity
As Peter Taylor said in the comments, you can solve subset sum with this:
- You have an instance of subset sum (hence a set S of integers and a number x)
- L := S + [0, ..., 0] (|S| times a zero), N := x, O := {+:|S|-1, *: |S| - 1, /:0, -: 0}
- Now solve this instance of my problem
- The solution for subset sum is the numbers of S that don't get multiplied with zero.
If you find an Algorithm that is better than O(2^n), you prove that P=NP. As P vs NP is a Millennium Prize Problem and hence worth 1,000,000 US-Dollar, it is very unlikely that somebody finds a solution for this. So I removed this part of the ranking.
Test cases
The following are not the only valid answers, other solutions exist, and are permitted:
- (
[17,5,9,17,59,14]
,{+:7, -:3, *:5, /:1}
,569
)
=>59 * (17-5)- 9 * 17 + 14 = 569
- (
[2,2]
,{'+':3, '-':3, '*':3, '/':3}
,1
)
=>2/2 = 1
- (
[2,3,5,7,10,0,0,0,0,0,0,0]
,{'+':20, '-':20, '*':20, '/':20}
,16
)
=>5+10-2*3+7+0+0+0+0+0+0+0 = 16
- (
[2,3,5,7,10,0,0,0,0,0,0,0]
,{'+':20, '-':20, '*':20, '/':20}
,15
)
=>5+10+0*(2+3+7)+0+0+0+0+0+0 = 15
Is
m = |L|
? If yes, how can you expect the runtime to not depend on the size of that list? For example,[2,2],[+,+,...,+,/],1
. In fact, since n is O(m), you might just write it all in terms of m. – boothby – 2013-02-26T09:31:13.5003What kind of arithmetic is this to use – exact fractionals, integer (
/
≡div
), just floating-point and hope-for-no-rounding-errors, ...? – ceased to turn counterclockwis – 2013-02-26T12:02:17.1404Why the complicated scoring rules for computational complexity? There's an easy reduction from subset-sum, so anything better than O(2^n) is worth a million USD. – Peter Taylor – 2013-02-26T13:14:48.127
@moose, I mistyped -- I meant "Is
m = |O|
?". As you affirm, my example (m-1 useless operators followed by one useful operator) shows that the problem is at least O(m): you can fix n and still make the runtime go off to infinity. – boothby – 2013-02-26T17:10:17.457@PeterTaylor: Thanks, I didn't see it. I've removed this part of the ranking and added an explanation. – Martin Thoma – 2013-02-26T18:29:52.110
Though I'd imagine this could actually be done in subexponential time with extra constraints to prevent "degenerate" cases like || times a zero. But that would probably be really complicated, so... – ceased to turn counterclockwis – 2013-02-26T20:26:30.087
1
Related: http://stackoverflow.com/questions/3947937/algorithm-for-permutations-of-operators-and-operands/3948113#3948113
– Dr. belisarius – 2013-02-27T11:51:16.763@belisarius Why don't you adapt your prior solution to the challenge at hand? At the very least it will provide an instructive look at how to avoid parentheses (through reverse Polish notation). – DavidC – 2013-02-27T15:58:34.403
@DavidCarraher I'll try. Need some spare time to do it :( – Dr. belisarius – 2013-02-27T17:03:58.763
13rd test case is not False...
5+10+2*3+7*0+0...
– Shmiddty – 2013-02-27T18:36:33.990It looks like the combination of 0,2,3,5,7,10 (with no practical restriction on operators) can actually generate quite a few numbers (pretty sure every whole number below 30, though I've only been doing the math in my head) – Shmiddty – 2013-02-27T20:03:21.387
@DavidCarraher I found that any short enough answer is too inefficient to be used with the OP's testcases. – Dr. belisarius – 2013-03-03T07:23:19.187