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".
 
Joined
May 11, 2022
Messages
60
Reaction score
6
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:
Joined
Jul 11, 2022
Messages
2
Reaction score
0
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.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top