用Python来求解质数
一个小学数学题目,题目不难,其实是一道逻辑题。

前些时候帮一个小学生讲解了几道数学题目,感觉小学的数学题很多时候是在考阅读理解和逻辑思维。数学题目不会做,难在于阅读水平和逻辑思维能力不够。
这道题目是这样求解的:

这个题目虽然我很快做出来了,但是在写程序的时候,一开始还是有点卡壳,不知道该如何下手。其实编程是一个更严格的逻辑思维的过程。后来想了一下,就是把自己的思维过程程序化,于是程序就写出来了。
程序运行的结果如下:
两位数的质数包括: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
step1 甲:我不知道这个质数是多少;
被删除的数据是: [97]
剩下的数据是: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89]
step2 乙:我早就知道你不可能知道;
被删除的数据是: [17, 37, 47, 67]
剩下的数据是: [11, 13, 19, 23, 29, 31, 41, 43, 53, 59, 61, 71, 73, 79, 83, 89]
step3 甲:我还是不知道;
被删除的数据是: [31, 61]
剩下的数据是: [11, 13, 19, 23, 29, 41, 43, 53, 59, 71, 73, 79, 83, 89]
step4 乙:我也早就知道你刚才也不可能知道;
被删除的数据是: [11, 41, 71]
剩下的数据是: [13, 19, 23, 29, 43, 53, 59, 73, 79, 83, 89]
step5 甲:我终于知道了。
目标结果数据是: [43]
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 2 20:42:45 2019
solve for prime number
老师把一个两位质数的十位数告诉了甲,个位数告诉了乙,两人有如下对话:
step1 甲:我不知道这个质数是多少;
step2 乙:我早就知道你不可能知道;
step3 甲:我还是不知道;
step4 乙:我也早就知道你刚才也不可能知道;
step5 甲:我终于知道了。
那么这个两位质数是多少?
"""
#step 0: 获取10-100范围内的所有质数
primelist=[]
import math
import pandas as pd
i=2
for i in range(10,100):
j=2
for j in range(2,int(math.sqrt(i))+1):
if(i%j==0):
break
else:
primelist.append(i)
print('两位数的质数包括:',primelist)
def listcount(primelist):
#求十位数和个位数的数量
acount=pd.DataFrame([[1,2,3,4,5,6,7,8,9],[0,0,0,0,0,0,0,0,0]]).T
bcount=pd.DataFrame([[1,3,7,9],[0,0,0,0]]).T
for x in primelist:
for i in range(acount.shape[0]):
if acount.iloc[i][0]==x//10:
acount.iloc[i][1]=acount.iloc[i][1]+1
for i in range(bcount.shape[0]):
if bcount.iloc[i][0]==x%10:
bcount.iloc[i][1]=bcount.iloc[i][1]+1
return (acount,bcount)
def prime_drop_ten(primelist,acount):
#将十位数只有1个的质数排除掉。
droplist=[]
for x in primelist:
for i in range(acount.shape[0]):
if acount.iloc[i][1]==1 and acount.iloc[i][0]==x//10:
primelist.remove(x)
droplist.append(x)
return primelist,droplist
def prime_drop_bit(primelist,droplist):
#剩下的质数list中,找出十位数只有1个的质数所对应的个位数,将这些个位数所对应的质数排除。
blist=[]
for x in droplist:
if x%10 not in blist:
blist.append(x%10)
droplist=[]
for x in primelist:
if x%10 in blist:
primelist.remove(x)
droplist.append(x)
return primelist,droplist
# step1:甲:我不知道这个质数是多少;
acount,bcount=listcount(primelist)
primelist,droplist=prime_drop_ten(primelist,acount)
print("\nstep1 甲:我不知道这个质数是多少;","\n被删除的数据是:",droplist,"\n剩下的数据是:",primelist)
# step2 乙:我早就知道你不可能知道;
primelist,droplist=prime_drop_bit(primelist,droplist)
print("\nstep2 乙:我早就知道你不可能知道;","\n被删除的数据是:",droplist,"\n剩下的数据是:",primelist)
#%%step3 甲:我还是不知道;
#剩下的质数list中,十位数不止1个,将十位数只有1个的排除掉。
acount,bcount=listcount(primelist)
primelist,droplist=prime_drop_ten(primelist,acount)
print("\nstep3 甲:我还是不知道;","\n被删除的数据是:",droplist,"\n剩下的数据是:",primelist)
#step4 乙:我也早就知道你刚才也不可能知道;
primelist,droplist=prime_drop_bit(primelist,droplist)
print("\nstep4 乙:我也早就知道你刚才也不可能知道;","\n被删除的数据是:",droplist,"\n剩下的数据是:",primelist)
#step5 甲:我终于知道了。
acount,bcount=listcount(primelist)
droplist=[]
slist=[]
for x in primelist:
for i in range(acount.shape[0]):
if acount.iloc[i][1]==1 and acount.iloc[i][0]==x//10:
slist.append(x)
if len(slist)==1:
print('\nstep5 甲:我终于知道了。\n目标结果数据是:',slist)
else:
print('result not found, the potential data are:',slist)
摘抄自网络,便于检索查找。

浙公网安备 33010602011771号