C D03 - Prime Testing - 3 Python Hackerrank

 You are given Q different queries. Each query consists of one number each i.e. N. You are to write a program that, for each query tests whether the number is prime or not. You must output Q different lines to stdout, ith line being "yes" if the N for ith query is a prime number and "no" otherwise.

Input Format

First line contains one integer, the number of queries Q.
Next Q lines contain one integer each, the N for the queries.

Constraints

1 <= Q <= 10^6
1 <= N <= 10^6

Output Format

Output Q lines, on each line you must print "yes" or "no" depending on the primality of the N in the query.

Sample Input 0

5
1
2
3
4
5

Sample Output 0

no
yes
yes
no
yes
Source Code:
# Enter your code here. Read input from STDIN. Print output to STDOUT
Q = int(input())
largest = 0
number =[]
for i in range(Q):
    number.append(int(input()))
    if(largest < number[i]):
        largest = number[i]
data =[]
for i in range(largest + 1):
    data.append(0)
    
data[0]=1
data[1]=1






for i in range(2 , largest  +1):
    if(data[i] == 0):
        j=i+i
        while(j<=largest):
            data[j]=1
            j=j+i
            
for i in range(Q):
    if(data[number[i]]==0):
        print("yes")
    else:
        print("no")
        

Comments