- N is a positive integer having exactly 5 distinct digits from 1 to 9 inclusively.
- Two of the digits of N are perfect squares.
- Two of the digits of N are prime numbers.
- One of the digits in N is neither a prime number nor a perfect square.
- Numerical value of third digit from the left is precisely twice that of the fifth digit.
- The fourth digit from the left is precisely 6 greater than the second digit.
- Reading from the left, the last digit is precisely 3 less than the first digit.
Determine the value of N.
Of the 6 constraints, the last three narrow it down to just 6 possible values for the first digit and three possible values for the second digit. The last 3 digits are fully determined by the first two. So there are only 36 possible solutions, before even considering the first three constraints.
The solution is unique:
73894--------------
squares = [1,4,9]
primes = [2,3,5,7]
neither = [6,8]
for a in range(4,10):
for b in range(1,4):
d = b+6
e = a-3
c = 2*e
digits = [a,b,c,d,e]
if len(digits) != len(set(digits)):
continue
s_count = 0
for i in digits:
if i in squares:
s_count += 1
if s_count != 2:
continue
p_count = 0
for i in digits:
if i in primes:
p_count += 1
if p_count != 2:
continue
n_count = 0
for i in digits:
if i in neither:
n_count += 1
if n_count != 1:
continue
stringDigits = [str(i) for i in digits]
print(int(''.join(stringDigits)))
|
Posted by Larry
on 2023-04-08 10:50:19 |