tags:

views:

1471

answers:

9

what will be the best way to find a prime number so that the time complexity is much reduced.

+16  A: 

Use sieve of Eratosthenes is if you want to enumerate primes. If you want to generate a large prime, generate a random odd number and check for primality.

avakar
Beat me to it. +1
Kirschstein
Wikipedia points to the Sieve of Atkin as being more efficient than the Sieve of Eratosthenes...
Thomas Owens
Thomas, I've removed the claim of optimality. Thanks.
avakar
+1. The Sieve of Eratosthenes is supposed to be fairly easy to implement, although I've never done so...perhaps I will, just to see. It might not be the best performer, but from what I can tell, it's usually good enough.
Thomas Owens
+12  A: 
JRL
+22  A: 

When it comes to finding prime numbers, the Sieve of Eratosthenes and the Sieve of Atkin are two possible solutions. The Sieve of Eratosthenes has a complexity of O((n log n)(log log n)). The Sieve of Atkin has a complexity of O(N / log log n).

If you have a number and you want to find out if it's prime, that is called performing a primality test. The naive approach is to check all numbers m from 2 to sqrt(n) and verify that n % m is not 0. If you want to expand this slightly, you can throw out all even numbers (except 2). There are also some other enhancements to this naive approach that might improve performance, along with other, more advanced techniques.

Thomas Owens
+1, I've never heard of sieve of Atkins before. Thanks.
avakar
Throw out the even number apart from 2...
Jonathan Leffler
Jonathan - Thanks for pointing that out. I edited the post to reflect that.
Thomas Owens
+7  A: 

Inspired by xkcd:

int findPrimeNumber() {
    return 2; // guaranteed to be prime
}
Dan Tao
If you're going to post a purely humorous answer to a serious question, at least make it community wiki.
Mark Ransom
I would come back with the smart-alecky response that this IS a serious answer... but really I just didn't know you could make answers community wiki (and that I honestly did not expect any upvotes for such a ridiculous answer).
Dan Tao
ahh crap you beat me to it. lol
Neil N
To be honest, it does satisfy the OP's request. However, my finely honed sense of telepathy, gained by trying to get requirements out of users without violating the Hague and Geneva Conventions, suggests that this isn't all the OP wants.
David Thornley
huh, would you look at that, you can make answers CW now! How long have I been blind to that feature?
Matt
But can you post this in Objective C?
Alex Reynolds
+3  A: 

If you want to generate primes from 1 to whatever, then the fastest way is probably a wheeled Sieve as implemented here, which can typically test more than 3,000,000 candidate primes a second on an average laptop (and that's using an unoptimized language like VB.net), and factor the non-primes to boot. In c++ it could be easily 5 to 20 times faster.

RBarryYoung
Although I'm not a fan of VB.NET, the "5-20" times faster seems a little over the top. But... +1 for the link
Philippe Leybaert
Granted it's hard to say: but my understanding is that VB.net does no loop, etc. code-type optimizations and this program/algorithm is *exactly* the kind of algorithm can go gangbusters on. OTOH, if it were making heavy use of non-replaceable .NET calls, then the difference would be a lot less.
RBarryYoung
(wish I was more proficient in VC++, I'd just re-code it and see what the difference was).
RBarryYoung
OTOH .NET uses jit which for a repetetive task like this should be able to optimize quite a bit.
Esben Skov Pedersen
Hmm, where could I find out more about jit optimizations?
RBarryYoung
RBarryYoung, see "Is Managed Code Slower Than Unmanaged Code?" http://www.grimes.demon.co.uk/dotnet/man_unman.htm
Alex
thanx Alex. ...
RBarryYoung
+1  A: 

Although there are more efficient algorithms, the Miller-Rabin primality test is one of the simplest tests to implement.

Brian
+1  A: 

There are two different questions:

1) How to find if a number is a prime number? If you discover an efficient algorithm for this one, you will be famous for the next 2000 years ;)

2) How to find the prime numbers up to a limit N?

probably this is what you are asking about. Sieve of Atkin is the most efficient one If your range or limit N is really big number. In reasonable ranges, you could implement an optimized variation of Sieve of Eratosthenes. I found these two sites to be more than useful:

EDIT: @avakar

While I am more than beginner on the subject, I don't think AKS is the waited algorithm! From the same source:

However, some composite numbers also satisfy the equivalence. The proof of correctness for AKS consists of showing that there exists a suitably small r and suitably small set of integers A such that if the equivalence holds for all such a in A then n must be prime.

AraK
There is an efficient (as in polynomial) algorithm: http://en.wikipedia.org/wiki/AKS_primality_test
avakar
-1: There already *are* extremely efficient algorithms for this. Sub-linear, for serial tests.
RBarryYoung
What is described in the "Black Key Sieve" is just a primitive version of a wheeled sieve.
RBarryYoung
(took the -1 back, considering there is some explanation of the differences)
RBarryYoung
AraK, AKS will polynomially and deterministically determine whether an integer is a prime. The two sentences you cited merely point out what must be proven in order for the algorithm to be considered correct. (And the original paper provides the proof.)
avakar
A: 

Take a look at existing libraries e.g. OpenSSL and GNU MP.

A: 
Ashishscores