Clear this python question


Joined
Jun 18, 2022
Messages
1
Reaction score
0
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".
 
Ad

Advertisements

Joined
May 11, 2022
Messages
28
Reaction score
0
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:

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.

Ask a Question

Top