Alex has before him some positive whole numbers, each consisting of a single digit, which may be repeated. The digit is different for each number, and the number of times it is repeated is also different for each number.
The sum of Alex's numbers is a number in which each digit is larger than the digit on its left, and it is the largest number for which this is possible, given the constraints described above.
What is the sum of Alex's numbers?
Note: Adapted from Enigma number:1765 which appeared on "New Scientist" in 2013.
689 = 1 + 22 + 666 meets all the requirements.
-----------------
def isStrictIncr(n, strict = True):
""" input integer and determine if digits are strictly increasing
set second parameter to False to remove the strict restriction"""
n=int(n)
l = list(str(n))
for i,c in enumerate(l):
if i == 0:
x = c
else:
if strict:
if c <= x:
return False
else:
x = c
else:
if c < x:
return False
else:
x = c
return True
sols = {}
for a in range(1,10):
for x in [int(str(a)*p) for p in range(1,10)]:
for b in range(a+1,10):
for y in [int(str(b)*q) for q in range(1,10)]:
if len(str(y)) == len(str(x)):
continue
for c in range(b+1,10):
for z in [int(str(c)*r) for r in range(1,10)]:
if len(str(z)) == len(str(x)):
continue
if len(str(z)) == len(str(y)):
continue
mysum = x+y+z
if not isStrictIncr(mysum):
continue
if mysum not in sols:
sols[mysum] = [[x,y,z]]
else:
sols[mysum].append([x,y,z])
ordered_sols = sorted(list(sols.keys()))[::-1]
print(sols[ordered_sols[0]])
|
Posted by Larry
on 2024-05-28 17:55:33 |