import itertools coins = [9, 2, 3, 5, 7] combinations = itertools.permutations(coins,len(coins)) for elem in combinations: A = elem[0] B = elem[1] C = elem[2] D = elem[3] E = elem[4] if(A + B*pow(C,2) + pow(D,3) - E == 399): print A print B print C print D print E