Project Euler Problem 51

Problem 51

By replacing the 1st digit of the 2-digit number *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

将两位数*3的第一个数字代换为任意数字,在九个可能值中有六个是素数:13、23、43、53、73和83。

将五位数56**3的第三和第四位数字代换为相同的任意数字,就得到了十个可能值中有七个是素数的最小例子,这个素数族是:56003、56113、56333、56443、56663、56773和56993。56003作为这一族中最小的成员,也是最小的满足这个性质的素数。

通过将部分数字(不一定相邻)代换为相同的任意数字,有时能够得到八个素数,求满足这一性质的最小素数。


def isprime(n):
    if n < 2:
        return False
    for x in range(2, int(n**0.5 + 1)):
        if n % x == 0:
            return False
    return True

def solve():
    mode = ['aaabb','aabab','aabba','ababa','abbaa',
            'abaab','babaa','baaba','baaab','bbaaa']
    for stri in mode:
        for i in range(10):
            stri1 = stri.replace('b', str(i), 1)
            for j in range(10):
                stri2 = stri1.replace('b', str(j), 1)
                for m in [1,3,7,9]:
                    stri3 = stri2 + str(m)
                    count = 0
                    for k in range(10):
                        stri4 = stri3.replace('a',str(k))
                        if stri4[0] == '0':
                            continue
                        if isprime(int(stri4)):
                            count += 1
                            if count == 8:
                                print(stri4)
                                

solve()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值