Last active
January 28, 2020 14:53
-
-
Save zeruns/50d2107b29c5316503985a718709e37b to your computer and use it in GitHub Desktop.
Python多种方法求某个范围内的所有素数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #方法1 | |
| def primeNUM(min,max): | |
| if min==1: | |
| print('') | |
| min += 1 | |
| for i in range(min, max+1): | |
| for j in range(2, i + 1): | |
| if i % j == 0: #判断i能不能被整除 | |
| break #退出for循环 | |
| if j == i: #若j等于i,说明i是素数 | |
| print(i,end=" ") | |
| print('') | |
| primeNUM(1,200) | |
| #方法2 | |
| def test(num): | |
| list = [] #定义一个列表 用于存储计算的数 | |
| i = num -1 # 去除本身 | |
| while i > 1: # 去除1 | |
| if num %i == 0 : #判断是否有余数 | |
| list.append(i) # 将所有的能整除i的数加入列表 | |
| i -= 1 | |
| if len(list) == 0 and num != 1: # 如果列表为空 就是表示除了1和它本身能整除 | |
| print(num,end=' ') | |
| def primeNUM2(min,max): | |
| j = min | |
| while j < max: | |
| test(j) | |
| j += 1 | |
| print('') | |
| primeNUM2(1,100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment