1 Star 0 Fork 0

阿坚/projecteuler

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
p041.py 8.00 KB
一键复制 编辑 原始数据 按行查看 历史
"""
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.
'''
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/kuperain/projecteuler.git
git@gitee.com:kuperain/projecteuler.git
kuperain
projecteuler
projecteuler
master

搜索帮助