0

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++?

Community
  • 1
  • 1
koder
  • 325
  • 3
  • 13

2 Answers2

1

One tip is that you can ignore any even numbers (so add 2 at a time when either looking for factors or checking values).

Tim Almond
  • 12,088
  • 10
  • 40
  • 50
1

http://en.wikipedia.org/wiki/Primality_test has all you need.

Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38