[EMAIL PROTECTED] writes: > Unfortunately I realise I stated the problem imprecisely. You're only > allowed to use each number once (otherwise there's a trivial solution > for every problem, i.e. x/x + x/x + x/x + ... + x/x repeated y times > for target y given any source number x). Trying your program on 234 > from [100,9,7,6,3,1] gives you 9*9*3-9 using the 9 three times.
Here's an inefficient solution, that doesn't find 234 but finds 253. If you see a double post, it's because I posted something similar a little while ago but cancelled it since it had a bug. I'm not sure this one is correct either ;-). from operator import * def countdown(nums, trace='', ops=[add,mul,sub,div]): n0,n1s = nums[0], nums[1:] if not n1s: yield n0, str(n0) return for f in ops: for r,t in countdown(n1s, trace, [add, mul, sub]): if f != div or r != 0 and n0 % r == 0: yield f(n0, r), '%s(%s,%s)'% (f.__name__, n0, t) def find_repr(target, nums): # print all representations of target from nums for x,t in countdown(nums): if x == target: print x,t find_repr(253, [100,9,7,6,3,1]) -- http://mail.python.org/mailman/listinfo/python-list