Clear this python question

Nuhajunaith

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".

phillip1882

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")``````

Last edited:

Anushka00

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