没有白走的路,每一步都算数🎈🎈🎈
质数,也叫做素数,比如2,3,5,7,11,13,17,19等都是质数,2,3,5,7是纯质数,而11,13,17,19,23并不是纯质数,当然375也不是纯质数,因为其首先不满足是质数。所以纯质数即是质数的每个位子都是质数。
没有任何输入
输出所有的个数
暴力算法:
直接采用暴力算法测试,时间超过,直接打印输出结果。
期间没有考虑到一些约束条件,比如说最大值20210605,即程序的最后一个for循环输出结果偏多。
import os
import sys
import math
def prime(n):
if n ==1:
return 0
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
return 0
return 1
##print(prime(2))
L = [2,3,5,7]
cnt = 4
for i in [2,3,5,7]:
for j in [2,3,5,7]:
s = i*10+j
if prime(s):
## print(s)
cnt+=1
for i in [2,3,5,7]:
for j in [2,3,5,7]:
for k in [2,3,5,7]:
s = i*100+j*10+k
if prime(s):
cnt+=1
## print(s)
for i in [2,3,5,7]:
for j in [2,3,5,7]:
for k in[2,3,5,7]:
for l in [2,3,5,7]:
s = i*1000+j*100+k*10+l
if prime(s):
## print(s)
cnt+=1
for i in L:
for j in L:
for k in L:
for l in L:
for m in L:
s = 10000*i+j*1000+k*100+l*10+m
if prime(s):
cnt+=1
for i in L:
for j in L:
for k in L:
for l in L:
for m in L:
for n in L:
s = 100000*i+j*10000+k*1000+l*100+m*10+n
if prime(s):
cnt+=1
for i in L:
for j in L:
for k in L:
for l in L:
for m in L:
for n in L:
for o in L:
s = 1000000*i+j*100000+k*10000+l*1000+m*100+n*10+o
if prime(s):
cnt+=1
for i in L:
for j in L:
for k in L:
for l in L:
for m in L:
for n in L:
for o in L:
for p in L:
s = 10000000*i+j*1000000+k*100000+l*10000+m*1000+n*100+o*10+p
if prime(s) and s<=20210605:
cnt+=1
print(cnt)
摘自《三体》:
你的无畏来源于无知。