Good to see you try this one! There are two issues here.

First, if you look at the last digit in ‘wronk’ in your output, it is not k! Why is this? The answer is that you have included k in your permutation but you then redefine it in line 6 so the k in the permutation and the k you use are different!

Second, k shouldn’t be in the permutation anyway and its value can be any digit (0 .. 9), not just those used.

Hence we get:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from itertools import permutations for p in permutations(range(10), 8): a, g, i, n, w, r, o, d = p if a and w: # let G in WRONG be incorrect with the actual value k for k in range(10): again = n + i * 10 + a * 100 + g * 1000 + a * 10000 wronk = k + n * 10 + o * 100 + r * 1000 + w * 10000 adding = g + n * 10 + i * 100 + d * 1000 + d * 10000 + a * 100000 if again + wronk == adding and adding % 2 == 0: print('(a, g, i, n, w, r, o, d, k) =', p + (k,)) print(again, '+', wronk, '=', adding) |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from itertools import permutations for p in permutations(range(10),9): # let k = g in wrong a,g,i,n,w,r,o,d,k = p for k in (a,g,i,n,w,o,r,d): if a!=0 and w != 0: again = n + i*10 + a*100 + g*1000 + a *10000 wronk = k + n*10 + o*100 + r*1000 + w*10000 adding = g + n*10 + i*100 + d*1000 + d*10000 + a*100000 if again + wronk == adding and adding % 2 == 0: print('a,g,i,n,w,r,o,d,k =',p) print(again, '+', wronk, '=', adding) |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
from itertools import permutations def sum_out(again, wrong, adding): print(' {:d}'.format(again)) print('+ {:d}'.format(wrong)) print('-------') print(' {:d}'.format(adding)) print('-------') print() digits = set(range(10)) # Considering the three units digits (N, G, G), if this sum is correct # then N must be zero and this means that I is also zero from the tens # digit sum. So the unit digit sum is incorrect. If the N is incorrect # we again find that N = I = 0 so one of the two G's is incorrect. So # I = 0 with no carry or I = 9 with a carry. Since the top digits of # the sum are correct A must be 1 and W must be 8 or or 9. A = 1 for I, W in (0, 8), (0, 9), (9, 8): # permute the remaining digits for the 5 remaining letters for D, G, N, O, R in permutations(digits - set((I, A, W)), 5): # one of the two G's in the units position is correct so # either G or N + G must be even, they can't both be odd if G % 2 and (N + G) % 2: continue # find the three correct values again = 10100 * A + 1000 * G + 10 * I + N wrong = 10000 * W + 1000 * R + 100 * O + 10 * N + G adding = 100000 * A + 11000 * D + 100 * I + 10 * N + G # now consider the sum without the units column and look for # results that are the same or differ only by a carry of one carry = adding // 10 - (again // 10 + wrong // 10) if carry in (0, 1): # if the G in ADDING is incorrect, see if there is a valid # digit at this position that gives a correct sum dgt = (again + wrong) - (adding - G) if dgt % 10 in (0, 2, 4, 6, 8) and dgt // 10 == carry: sum_out(again, wrong, adding - G + dgt) # if the G in WRONG is incorrect, see if there is a valid # digit at this position that gives a correct sum dgt = adding - (again + wrong - G) if not G % 2 and dgt in range(10): sum_out(again, wrong - G + dgt, adding) |