[Python]判断素数
sub from:http://bbs.fishc.com/thread-57238-1-1.html
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ## 素数 2 def Isprime0(var_num): ##判断是否是素数 是素数返回1 否则返回0 3 if (var_num <= 1): 4 ret_val = 0 5 else: 6 index = 2 7 while True : 8 if(var_num <= index): 9 ret_val = 1 10 break 11 if(var_num % index == 0): 12 ret_val = 0 13 break 14 index = index + 1 15 return ret_val 16 17 def Isprime1(var_num):##判断是否是素数 是素数返回1 否则返回0 18 if (var_num <= 1): 19 return 0 20 y = [] 21 for i in range(2,var_num): 22 if not(var_num%i): 23 y.append(var_num) 24 return not(y) #空列表取not为True,非空列表取not为False 25 26 def Isprime2(var_num):##判断是否是素数 是素数返回1 否则返回0 27 if (var_num <= 1): 28 return 0 29 if (0 in [var_num%d for d in range(2,var_num)]): 30 return 0 31 else: 32 return 1 33 34 def Isprime3(var_num):##判断是否是素数 是素数返回1 否则返回0 35 if (var_num <= 1): 36 return 0 37 var_ret = 0 if 0 in [var_num%d for d in range(2,var_num)] else 1 38 return var_ret 39 40 def Isprime4(var_num):##判断是否是素数 是素数返回1 否则返回0 41 y = [i for i in range(1,var_num+1) if not(var_num%i)] 42 if (len(y) == 2): 43 return 1 44 else: 45 return 0 46 47 ################# 48 total = 5*[0] 49 Isprime = [Isprime0,Isprime1,Isprime2,Isprime3,Isprime4] ##函数列表 50 for i in range(1,1000): 51 for j in range(0,5): 52 if Isprime[j](i) == 1: 53 total[j] += 1 54 print(total) 55 ## 56 input("按任意键退出")
~不再更新,都不让我写公式,博客园太拉胯了