# # #
+ # # #
--------
# # #
--------
Using each digit from 1 to 9 exactly once, what is the largest sum you can obtain in the addition above?
The largest such sum is 981.
The program below finds 168 different solutions, not counting an equal number if the addends are swapped.
There are 31 different sums which can be created:
[459, 468, 486, 495, 549, 567, 576, 594, 639, 648, 657, 675, 693, 729, 738, 783, 792, 819, 837, 846, 864, 873, 891, 918, 927, 936, 945, 954, 963, 972, 981]
Each sum can be obtained either 4 ways (20 of them) or 8 ways (11 of them).
There are 8 solutions which share the largest sum, 981:
235 + 746 = 981
236 + 745 = 981
245 + 736 = 981
246 + 735 = 981
324 + 657 = 981
327 + 654 = 981
354 + 627 = 981
357 + 624 = 981
--------------------
from itertools import permutations
sums_list = []
for p in permutations('123456789'):
top = int(p[0]+p[1]+p[2])
mid = int(p[3]+p[4]+p[5])
thesum = int(p[6]+p[7]+p[8])
if top + mid != thesum:
continue
ans = sorted([top,mid,thesum])
if ans not in sums_list:
sums_list.append(ans)
mydict = {}
for trip in sums_list:
if trip[2] not in mydict:
mydict[trip[2]] = [trip]
else:
mydict[trip[2]].append(trip)
print(sorted(mydict.keys()))
print(len(mydict.keys()))
for t in mydict[max((mydict.keys()))]:
print('{} + {} = {}'.format(t[0],t[1],t[2]))
|
Posted by Larry
on 2024-01-15 11:57:55 |