PON - Prime or Not


Given the number, you are to answer the question: "Is it prime?"

Solutions to this problem can be submitted in C, C++, Pascal, Perl, Python, Ruby, Lisp, Hask, Ocaml, Prolog, Whitespace, Brainf**k and Intercal only.

Input

t – the number of test cases, then t test cases follows. [t <= 500]
Each line contains one integer: N [2 <= N <= 2^63-1]

Output

For each test case output string "YES" if given number is prime and "NO" otherwise.

Example

Input:
5
2
3
4
5
6

Output:
YES
YES
NO
YES
NO

hide comments
musfiq_mridha: 2017-06-17 16:20:38

Is it solvable only using sieve with optimazation?

Last edit: 2017-06-17 16:20:53
kspoj: 2017-06-10 09:35:01

Guys, I've tried using Miller Rabin test as suggested, but my code's failing in this test case :
10
5915587277
1500450271
3267000013
5754853343
4093082899
9576890767
3628273133
2860486313
5463458053
3367900313

all are primes, but my code is showing NO for some of these.
Could you please tell me what I should look out for.

( I'm using 500 iterations BTW)

Last edit: 2017-06-10 09:35:25
rohit9934: 2017-05-08 16:02:07

The only thing that can screw you up is overflow WA

caohoangtung: 2017-04-01 19:50:06

First time using Miller Rabin :D learnt alot

nilabja16180: 2017-03-25 15:41:52

20 iteration gives 0.15 sec, 100 iteration produces result in 0.74 sec, 1000 iteration produces result in 7.84 sec!

epsilonalpha: 2017-03-14 20:49:32

0.05s with 7-base deterministic Miller Rabin and 0.09s with 12-base deterministic Miller Rabin in C++. :)

thinkwise: 2017-02-22 17:52:36

Ohh man why i am getting tle man

cake_is_a_lie: 2017-02-22 01:11:21

@rishi_devan: We do have a deterministic primality test; google AKS primality test.

Note that I did get AC with Rabin-Miller however; it's just a matter of using enough rounds; and you can use deterministic witnesses to ensure consistent results.

Last edit: 2017-02-22 01:32:00
malavan: 2017-01-10 15:58:03

Why wrong answer in Miller :(

scorpion_ajay: 2017-01-07 14:24:36

good question, ac in one go, 20 iterations is a good choice for almost all integers
please tell me, why sieve did not worked here
ac in a go :)


Added by:Roman Sol
Date:2005-01-24
Time limit:21s
Source limit:5000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ADA95 ASM32 BASH CSHARP CLPS D ERL FORTRAN ICON JAVA JS-RHINO LUA NEM NICE PHP PIKE ST
Resource:ZCon 2005