-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_prime.py
38 lines (29 loc) · 878 Bytes
/
is_prime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding:utf-8 -*-
# __author = 'c08762'
"""输入1个数,输出不大于该数的所有质数"""
from math import sqrt
def is_prime(n):
"""This function return a number is a prime or not"""
assert n >= 2
for i in range(2, int(sqrt(n))+1):
if n % i == 0:
return False
return True
def get_num():
"""This function get a integer greater than 1 from input"""
while True:
try:
num = int(input('Pls enter a integer greater than 1:\n'))
except ValueError:
print("NOT a number, try again:\n")
continue
if num <= 1:
print('NOT greater than 1, try again:\n')
continue
return num
def put_prime(numb):
for x in range(2, numb+1):
if is_prime(x):
print(x, end=',')
if __name__ == '__main__':
put_prime(get_num())