编写程序,输出所有有1、2、3、4这四个数字组成的素数,并且在每个素数中每个数字只使用一次。

编写程序,输出所有有1、2、3、4这四个数字组成的素数,并且在每个素数中每个数字只使用一次(仅使用列表)。

#逻辑直白版:
alist = []
for i in range(2,4322):
    for j in range(2,i):
        if i%j==0:
            break
    else:
        alist.append(i)
blist = []
for item in alist:
    if len(str(item))==len(set(str(item))):
        blist.append(item)
clist = []
for item in blist:
    for i in str(item):
        if i not in '1234':
            break
    else:
        clist.append(item)
print(clist)  
#逻辑合并版
alist = []
for item in range(2,4322):
    for j in range(2,item):
        if item%j==0:
            break
    else:
        if len(str(item))==len(set(str(item))):
            for i in str(item):
                if i not in '1234':
                    break
            else:
                alist.append(item)
print(alist)  

tips:还可再优化,仅供参考

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年11月6日
下一篇 2023年11月6日

相关推荐