-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime_numbers.py
50 lines (35 loc) · 1.15 KB
/
prime_numbers.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
39
40
41
42
43
44
45
46
47
48
49
50
"""
Prime numbers
Write a Python program that outputs all the prime numbers
within a closed interval of numbers between lower and upper,
given by user input.
For example:
for lower=2 and upper=23 the output is:
Prime numbers between 2 and 23 are: 2 3 5 7 11 13 17 19 23
for lower=5 and upper=27 the output is:
Prime numbers between 5 and 27 are: 5 7 11 13 17 19 23
for lower=-2 and upper=5 the output is:
Prime numbers between -2 and 5 are: 2 3 5
@author: Luísa Maria
"""
def is_prime(num):
result = False
if (num < 2):
result = False
elif(num == 2):
result = True
else:
for i in range(2, num):
if num % i == 0:
result = False
break
else:
result = True
return result
lower = int(input("Lower: "))
upper = int(input("Upper: "))
prime_numbers = ""
for i in range(lower, upper + 1):
if(is_prime(i)):
prime_numbers = prime_numbers + " " + str(i)
print("Prime numbers between " + str(lower) + " and " + str(upper) + " are:" + prime_numbers)