Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- def find_sum_equal_n():
- n = 0
- num = range(10)
- solutions = set()
- for perm in permutations(num):
- tple = product_sum(perm)
- if product_num(tple) == n:
- tple = well_ordered(tple)
- solutions.add(tple)
- return list(solutions)
- def product_num(tple):
- total = tple[0]*tple[1] - tple[2]*tple[3]
- return total
- def product_sum(perm):
- num1 = 100*perm[0] + 10*perm[1] + perm[2]
- num2 = 10*perm[3] + perm[4]
- num3 = 100*perm[5] + 10*perm[6] + perm[7]
- num4 = 10*perm[8] + perm[9]
- return (num1, num2, num3, num4)
- def well_ordered(tple):
- a = max(tple[0], tple[1])
- b = min(tple[0], tple[1])
- c = max(tple[2], tple[3])
- d = min(tple[2], tple[3])
- if a > c:
- tple = (a,b,c,d)
- else:
- tple = (c,d,a,b)
- return tple
- def display_solutions(lst):
- print '============================================'
- for solution in lst:
- display_sum(solution)
- print '============================================'
- def display_sum(tple):
- print ' ' + str_num(tple[0], 3) + ' x ' + str_num(tple[1], 2)
- print ' - ' + str_num(tple[2], 3) + ' x ' + str_num(tple[3], 2)
- print ' = ', product_num(tple)
- def str_num(num, length):
- str_num = str(num)
- diff = max(length - len(str_num), 0)
- string = ' '*diff
- string += str_num
- return string
- if __name__ == '__main__':
- lst = find_sum_equal_n()
- display_solutions(lst)
- print len(lst)
Advertisement
Add Comment
Please, Sign In to add comment