C D02 - Prime Testing - 2 Python Hacker rank

 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^5
1 <= N <= 10^5

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

import math
t=int(input())
for i in range(t):
    n = int(input())
    if n == 1:
         print('no')
    elif n == 2:
        print('yes')
    else:
        for j in range(2,math.floor(math.sqrt(n)) + 1):
            if n % j==0:
                print("no")
                break
        else:
            print("yes")

Comments