I got one puzzle and I want to solve it using Python. Puzzle: A merchant has a 40 kg weight which he used in his shop. Once, it fell from his hands and was broken into 4 pieces. But surprisingly, now he can weigh any weight between 1 kg to 40 kg with the combination of these 4 pieces. So question is, what are weights of those 4 pieces? Now I wanted to solve this in Python. The only constraint i got from the puzzle is that sum of 4 pieces is 40. With that I could filter all the set of 4 values whose sum is 40. import itertools as it weight = 40 full = range(1,41) comb = [x for x in it.combinations(full,4) if sum(x)==40] length of comb = 297 Now I need to check each set of values in comb and try all the combination of operations. Eg if (a,b,c,d) is the first set of values in comb, I need to check a,b,c,d,a+b,a-b, .................a+b+c-d,a-b+c+d........ and so on. I tried a lot, but i am stuck at this stage, ie how to check all these combination of calculations to each set of 4 values. Question : 1) I think i need to get a list all possible combination of [a,b,c,d] and [+,-]. 2) does anyone have a better idea and tell me how to go forward from here? Also, I want to do it completely without help of any external libraries, need to use only standard libraries of python. EDIT : Sorry for the late info. Its answer is (1,3,9,27), which I found a few years back. I have checked and verified the answer. EDIT : At present, fraxel's answer works perfect with time = 0.16 ms. A better and faster approach is always welcome. Regards ARK Continue reading...