What is the highest prime number with no repeated digits?
Kudos to Charlie who noted the solution could not have 10 digits or it wouldn't be a prime (sod=45). Silly me, my program ran for hours getting from 10 digits down to 9, but ultimately found the same answer as the others: 987,654,103.
(re-written to start from 9 digits):
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
for n in range(987654321,1,-2):
s = str(n)
if len(s) > len(set(s)):
continue
if isprime(n):
print(n)
break
|
Posted by Larry
on 2020-12-12 15:00:40 |