Possible Duplicate:
Which is the fastest algorithm to find prime numbers?
What is the fastest way to check if a number is prime( large numbers). I have tried the standard method i.e running a loop till root(n) or (n/2) and checking if anything divides it. Also i have tried the sieve method. Is there anything better to implement in c++?
http://en.wikipedia.org/wiki/Primality_test has all you need.
One tip is that you can ignore any even numbers (so add 2 at a time when either looking for factors or checking values).
精彩评论