Two mathematicians, Rex and Ralph, have an ongoing competition to stump each other. Ralph was impressed by the ingenuity of Rex's last attempt using clues involving prime numbers, but he thinks he's got an even better one for Rex. He tells Rex he's thinking of a 6-digit number.
"All of the digits are different. The digital sum matches the number formed by the last two digits in the number. The sum of the first two digits is the same as the sum of the last two digits."
"Take the sum of the number, the number rotated one to the left, the number rotated one to the right, the number with the first three and last three digits swapped, the number with the digit pairs rotated to the left, and the number with the digit pairs rotated to the right. The first and last digits of this sum match the last two digits of the number, in some order."
Ralph then asks, "If each of the three numbers formed by the digit pairs in the number is prime, then what is the number?"
Rex looks confused, and for a moment Ralph thinks he's finally gotten him. Then Rex smiles, scribbles a few things down on a pad of paper and then says,
"Very nice, Ralph!"
Rex then tells Ralph his number.
What did Rex say?
Regarding the megasum of paragraph 2, I am assuming that the phrase "digit pairs rotated to the left" means that if the number is abcdef, the transformation is:
cdefab and not badcfe. This interpretation is equivalent to cycling the 6 digit number one digit left for 6 iterations and summing the results.
416723
"""
def rotationalsum(text):
if type(text) == tuple:
x = ''.join(text)
elif type(text) == str:
x = text
elif type(text) == int:
x = str(text)
tot = 0
for i in range(6):
tot += int(x)
x = x[1:] + x[0]
return [str(tot)[0], str(tot)[-1]]
def sod(n):
""" Input an integer. Returns the Sum of the Digits """
aList = list(str(n))
ans = 0
for c in aList:
ans = ans + int(c)
return ans
def isprime(n):
'''check if integer n is a prime'''
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
from itertools import permutations
for p in permutations('123456789',6):
a = p[0]
b = p[1]
c = p[2]
d = p[3]
e = p[4]
f = p[5]
if not isprime(int(a+b)):
continue
if not isprime(int(c+d)):
continue
if not isprime(int(e+f)):
continue
if (sod(int(''.join(p)))) != int(e+f):
continue
if int(a)+int(b) != int(e)+int(f):
continue
if rotationalsum(p) != [e,f] and rotationalsum(p) != [f,e]:
continue
print(int(''.join(p)))
|
Posted by Larry
on 2023-11-08 17:50:55 |