forked from abhaysamantni/Python_MidTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Midterm_Review_Q3.py
42 lines (40 loc) · 1.09 KB
/
Midterm_Review_Q3.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
# def primes(n):
# <Your code here>
#
# def isPrime(n):
# if n == 1:
# return False
# for t in range(2,n):
# if n % t == 0:
# return False
# return True
#
#
# x=primes()
# print(next(x))
# print(next(x))
# print(next(x))
# Write code for the generator function primes. This function takes one input n, which is the value from which you want
# to start the prime sequence. It returns the prime numbers, no less than the value of n and no greater than 100.
# Please note that you are required to write the function as a generator function.
# When you execute the above code, the output is 2, 3, 5. You are required to use the isPrime function that is provided
# to you above, to check if the current value of n is a prime number.
def primes(n=1):
while n < 100:
# yields n instead of returns n
if isPrime(n): yield n
# next call it will increment n by 1
n += 1
def isPrime(n):
if n == 1:
return False
for t in range(2,n):
if n % t == 0:
return False
return True
#
#
x=primes()
print(next(x))
print(next(x))
print(next(x))