The door to Prof. Adams
laboratory has one of those keypad
locks that requires entering five
digits to open. Unfortunately, he
has a hard time remembering the
combination, but he has figured
out a way to determine it.
The five
digits are all different, and he has
observed that the first two digits
form a perfect square, while the last
two digits form a smaller perfect
square. Also, the middle digit is the
smallest. If he arranges the five
digits to form all possible five-digit
integers (leading zeros allowed) and
adds all these numbers, the sum is
a palindrome, with each of its digits
a multiple of three.
What is the
combination?
I found a unique solution:
49236
------------------
from itertools import permutations
squares = [str(n**2) for n in range(4,10)]
dbm = []
def test(n):
scramble = 0
s = str(n)
if len(set(str(s))) != 5:
return False
for perm in permutations(s):
scramble += int(''.join(perm))
scr = str(scramble)
if scr != scr[::-1]:
return False
for ch in scr:
if ch not in '0369':
return False
dbm.append(n)
return True
for n in range(10000,100000):
test(n)
for d in dbm:
s = str(d)
if s[:2] not in squares:
continue
if s[3:] not in squares:
continue
if s[:2] <= s[3:]:
continue
if min(s) != s[2]:
continue
print(d)
|
Posted by Larry
on 2024-12-27 10:04:28 |