简体   繁体   中英

Verifiying if a number is prime or Not from a List of elements

I need to test whether the elements in an array are prime or not by individually verifying them and printing the same for them .

For this I need to take the number of elements Accept the elements in an array than I need to verify if the element in an array is prime or not

Please find my code below:

T = int(raw_input())
arr=[]
for i in range(0,T):
    arr.append(int(raw_input()))

for n in arr:
    for j in range(2, n):

        if n % j == 0:
            print 'Not prime'
            break

        else: 
            print "Prime"
            break

Input given:

2
31
33

Expected output:

   Prime
   Not prime

My Output:

Prime
Prime

What am I doing wrong here?

You're not looping through all the candidates since you always break on the first iteration, no matter the modulo. Instead break only in case that number is not a prime and if the loop completes then the number is prime:

for j in range(2, n):
    if n % j == 0:
        print 'Not prime'
        break
else: 
    print "Prime"

In your current code you are trying to print prime or not prime every single iteration of the loop. What you really want to do is loop through the numbers and if your number is divisible the number is not prime, if you manage to loop through the entire list without returning, then you know the number is prime.

This is best solved using an isPrime function

Put this at the top of your code

def isPrime(num):
    for i in range(2,num):
        if num % i == 0:
            return False
    return True

And then change the bottom to this:

for n in arr:
    if isPrime(n):
        print("Prime")
    else:
        print("Not prime")

You could use the AKS Algorithm:

def isprime(n):
    if n == 2:
        return "Prime"
    if n == 3:
        return "Prime"
    if n % 2 == 0:
        return "Not Prime"
    if n % 3 == 0:
        return "Not Prime"
    i = 5
    w = 2
    while i * i <= n:
        if n % i == 0:
            return "Not Prime"
        i += w
        w = 6 - w
    return "Prime"

Let us run the code manually for the input 33. As it enters the inner for loop for j in range(2,n) it exits after checking for j=2, since, 33%2 == 0 is not true, it enters the else statement, prints "Prime" and exits the inner for loop.

So the correct code would be :

T = int(raw_input())
arr=[]
for i in range(0,T):
    arr.append(int(raw_input()))

for n in arr:
    i = 0 # check variable = 1 if n is found to be prime else 0
    for j in range(2, n):

        if n % j == 0:
            i = 1 # found that n is prime. So set i to 1 
            print 'Not prime'
            break

    if i == 0: # check if n did not turn out to be divisible by any number 
        print 'prime'

Additional info : You can just check from 2 to square_root(n) ( x**0.5 in python) instead of checking up to n if you want to speed up your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM