There are many interesting results on this problem.
The naive method is testing odd factors up to n/2. It's O(n^2) though.
By eliminating multiples of 3, 5, 7 first you can eliminate about 70% of the numbers.
In testing, you can use square instead of square root to speed up calculation.
There are probabilistic algorithms, such as Rabin test, and Lehman test.
The Sieve of Atkin algorithm is described here. Cost is O((N/log) * log N) in time, and N^(1/2 + o(1)) in memory.
The sieve of Eratosthenes algorithm uses O(N) time and O(N^(1/2)(log (log N))/log N) memory.
BTW, the density of prime numbers around N is 1/ln(N). This translates to that the average gap between prime numbers around N is ln(N).
Friday, April 9, 2010
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2010
(67)
-
▼
April
(14)
- Find median
- Find Median of Two Sorted Arrays
- Online Algorithm Resources
- Max submatrix problem
- Check Equality of Two Number Sets
- Largest rectangle in histogram problem
- IR Evaluation
- Garbage collector implementation
- Find prime numbers
- Newton Ralphson's method to calculate square root
- The Frobenius Number
- Search in a circular sorted array
- The Maximal Rectangle Problem
- Young Tableau
-
▼
April
(14)
No comments:
Post a Comment