代码拉取完成,页面将自动刷新
"""
Problem 41: https://projecteuler.net/problem=41
Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits
1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
What is the largest n-digit pandigital prime that exists?
"""
# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/13
'''
import itertools
import p035
def isPandigitalPrime(m) -> bool:
'''
>>> assert isPandigitalPrime(2143)
'''
ms = str(m)
if '0' in ms: return False
d = len(ms)
return set(ms) == set('123456789'[:d]) and p035.isPrime(int(m))
def maxPandigitalPrime_d(d: int) -> int:
nums = sorted([''.join(item) for item
in itertools.permutations('123456789'[:d])], reverse = True)
for i in nums:
if isPandigitalPrime(i):
print(f'maxPandigitalPrime with {d}-digits: {i}')
return int(i)
print(f'PandigitalPrime with {d}-digits do not exist.')
return 0
def solution(limit:int = 9) ->int:
return max(maxPandigitalPrime_d(d) for d in range(2,limit+1))
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=False)
print(solution())
# 7652413
'''
PandigitalPrime with 2-digits do not exist.
PandigitalPrime with 3-digits do not exist.
maxPandigitalPrime with 4-digits: 4231
PandigitalPrime with 5-digits do not exist.
PandigitalPrime with 6-digits do not exist.
maxPandigitalPrime with 7-digits: 7652413
PandigitalPrime with 8-digits do not exist.
PandigitalPrime with 9-digits do not exist.
'''
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。