Complete the code to find if a given number is a prime number? The program will take a positive integer greater than 1 as input and indicate if it is a prime number by saying "prime", and if it is not a prime number saying "not a prime".

there's a wide variety of ways to do that, depending on how big of a number you're testing for.
i'll assume its less than a million.
so for this case, you can use trial division

Python:

Pcheck = input("number to test for: ")
chk = False
if Pcheck %2 == 0:
print("not prime")
chk = True
if Pcheck %3 == 0:
print("not prime")
chk = True
i = 5
while i*i <= Pcheck and chk ==False:
if Pcheck %i == 0:
print("not prime")
chk = True
i+= 2
if chk == False:
print("prime")

hi,
you can check if a given number is prime or not by checking the moduulo of the number through a loop.
The loop will run from 2 to square-root(number).
you can refer the code from this site: StackOverflow

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.