#!/usr/bin/env python import itertools coin_names = {9: "blue coin", 2: "red coin", 3: "corroded coin", 5: "shiny coin", 7: "concave coin"} 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 coin_names[A] print coin_names[B] print coin_names[C] print coin_names[D] print coin_names[E]