PYTHON 3
Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows:
A = 21212
B = A + 5000
Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29.
Rules:
You should first create a boolean function called isPrime and use that function in your program. This function should take in any int and return true if the number is prime, otherwise, return a false. In the main body of your program, you should create a loop from A to B (inclusive) and use isPrime function to determine if the loop number should be counted or not.
Your program SHOULD NOT PRINT the individual prime numbers. You can print them for your own testing purpose, but in the final submission, comment out such print statements.
Your program SHOULD ONLY PRINT the answer -- which is a number.
Do not print extra characters in your answer (E.g. "answer=123" instead of 123)
Code:
def isPrime(x):
if x >= 2:
for y in range(2,x):
if not ( x % y ):
return False
else:
return False
return True
def main():
A = 2
B = 29
count = 0
for i in range(A, B+1):
if isPrime(i):
count = count + 1
print(count)
main()
Code screenshot:
Output:
496
Get Answers For Free
Most questions answered within 1 hours.