import math
import random
oTemp = []
oList = []
i = 0
while True:
a = random.randint(1,4)
if a in oTemp:
continue
else:
oTemp.append(a)
i +=1
if i%4==0:
Num = oTemp[0]*1000+oTemp[1]*100+oTemp[2]*10+oTemp[3]
if Num in oList:
oTemp=[]
oList.append(Num)
if len(oList)==24:
break
for m in oList:
for n in range(2,int(math.sqrt(m))+1):
if m%n==0:
oList.remove(m)
print oList
這段程式碼是用1-4生成4位數,4個位上的數字不相同的素數。可以做下參考
import math
import random
oTemp = []
oList = []
i = 0
while True:
a = random.randint(1,4)
if a in oTemp:
continue
else:
oTemp.append(a)
i +=1
if i%4==0:
Num = oTemp[0]*1000+oTemp[1]*100+oTemp[2]*10+oTemp[3]
if Num in oList:
i = 0
oTemp=[]
continue
else:
oList.append(Num)
i = 0
oTemp=[]
if len(oList)==24:
break
for m in oList:
for n in range(2,int(math.sqrt(m))+1):
if m%n==0:
oList.remove(m)
break
print oList
這段程式碼是用1-4生成4位數,4個位上的數字不相同的素數。可以做下參考