1
   

Minimum Complexity to find Prime

 
 
vinsan
 
Reply Mon 19 Sep, 2005 11:14 am
What is the fastest algorithm to decide a number N to be prime

The one I have proposed is as follows

Code:boolean IsPrime(long N){
if (N==0 || N==1 || N%2==0) return false;

for (int i=1; i<=round(sqrt(N)/2); i++) {
if (N%(2*i+1)==0) return false;
}

return true;
}


The algorithm travels among all ODD numbers between 1 and Round(Sqrt(N)) and finds if they divide N or not.

Complexity O(N) = Round(Sqrt(N)/2)

So for a large numbers like for 10 digit prime number 7427466391 it will be 43092 iterations or so

What other fastest algorithms exist? Any direct formula?

Thanks
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 1 • Views: 2,501 • Replies: 3
No top replies

 
g day
 
  1  
Reply Tue 20 Sep, 2005 05:51 am
ECPP is the fastest known general-purpose primality testing algorithm. ECPP has a running time of O(ln(N)^4).

As of 2004, the program PRIMO can certify a 4769-digit prime in approximately 2000 hours of computation (or nearly three months of uninterrupted computation) on a 1 GHz processor using this technique. As of 2003, the largest prime certified using this technique had 6959 decimal digits and required 12658 CPU-hours on a 1.4 GHz Athlon (Martin).

http://mathworld.wolfram.com/EllipticCurvePrimalityProving.html

See also http://mathworld.wolfram.com/Pollardp-1FactorizationMethod.html for a simpler but still quite complex method that doesn't involve elliposids factoring.

PS

To run your algorithm alot faster for most optimising compilers:

1. check for N = 0, 1, 2, 3 or even - to speed things up
2. take the sqrt out of the loop invariant test, make it the starting loop value if its not a trivial test caught above
3. The loop if it is reached will run at least once so make the conditional test last - implying use a do..while loop rather than a for () or while () loop, will save one test vs other loop options
4. use !(N) to test for N==0, bitwise NOT is often faster than a compare to 0 on many compilers
5. wherever you have x*2^a use x<<a (left shift) so 2*x = x<<1 its faster using bitwise operations
6. the loop decrement can be moved into the if test as i-- or used in the loop conditional as while (--i) - note compilers are good with decrement skip on 0 loops optimisations


Code:
boolean IsPrime(unsigned long N){ register int i;
(N < 2 || !(N%2) )? return false : (N < 4)? return true : i=round(sqrt(N)/2);

do
if (!(N%((i<<1)+1)) ) return false;
while ( --i );

return true;
}


If you want probablistic sampling algorithims (so you trade certainity of a prime for confidence intervals) see http://mathworld.wolfram.com/PrimalityTest.html

for good background on this.

Primality Test

A primality test is a test to determine whether or not a given number is prime, as opposed to actually decomposing the number into its constituent prime factors (which is known as prime factorization).

Primality tests come in two varieties: deterministic and probabilistic. Deterministic tests determine with absolute certainty whether a number is prime. Examples of deterministic tests include the Lucas-Lehmer test and elliptic curve primality proving. Probabilistic tests can potentially (although with very small probability) falsely identify a composite number as prime (although not vice versa). However, they are in general much faster than deterministic tests. Numbers that have passed a probabilistic prime test are therefore properly referred to as probable primes until their primality can be demonstrated deterministically.

The Rabin-Miller strong pseudoprime test is a particularly efficient algorithm used by Mathematica's PrimeQ function. Like many such algorithms, it is a probabilistic test using pseudoprimes. In order to guarantee primality, a much slower deterministic algorithm must be used. However, no numbers are actually known that pass advanced probabilistic tests (such as Rabin-Miller) yet are actually composite.

Unlike prime factorization, primality testing was long believed to be a P-problem (Wagon 1991). This had not been demonstrated, however, until Agrawal et al. (2002) unexpectedly discovered a polynomial time algorithm for primality testing that has asymptotic complexity of O(ln(N)^12) (Bernstein 2002, Clark 2002, Indian Institute of Technology 2002, Pomerance 2002ab, Robinson 2002). Their algorithm has come to be called the AKS primality test. http://mathworld.wolfram.com/Atkin-Goldwasser-Kilian-MorainCertificate.html
0 Replies
 
vinsan
 
  1  
Reply Tue 20 Sep, 2005 07:22 am
Nice
GREAT HELP!!!

THX. Very Happy
0 Replies
 
markr
 
  1  
Reply Tue 20 Sep, 2005 09:34 am
I would pull the multiplication/shifting out of the loop and increment/decrement by two. Of course, your starting/ending condition changes (don't divide sqrt(N) by 2). In g__day's case, you must ensure that you start with an odd number.

I'd also consider incrementing by 2, 2, 4, 2, 2, 4, etc. There's a bit more overhead per loop, but you cut your mod calculations (expensive) by 33%.
0 Replies
 
 

Related Topics

Evolution 101 - Discussion by gungasnake
Typing Equations on a PC - Discussion by Brandon9000
The Future of Artificial Intelligence - Discussion by Brandon9000
The well known Mind vs Brain. - Discussion by crayon851
Scientists Offer Proof of 'Dark Matter' - Discussion by oralloy
Blue Saturn - Discussion by oralloy
Bald Eagle-DDT Myth Still Flying High - Discussion by gungasnake
DDT: A Weapon of Mass Survival - Discussion by gungasnake
 
  1. Forums
  2. » Minimum Complexity to find Prime
Copyright © 2025 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.21 seconds on 02/05/2025 at 04:00:58