Miller-rabin Prime number detection algorithm

Source: Internet
Author: User

Description:

Goldbach ' s conjecture is one of the oldest and best-known unsolved problems in number theory and all of mathematics. It states:

Every even integer greater than 2 can be expressed as the sum of the primes.

The actual verification of the Goldbach conjecture shows that even numbers below at least 1e14 can be expressed as a sum O f The prime numbers.

Many times, there is more than one-to-represent even numbers as-prime numbers.

For example, 18=5+13=7+11, 64=3+61=5+59=11+53=17+47=23+41, etc.

Now this problem are asking you to divide a postive even an integer n (2<n<2^63) into the prime numbers.

Although a certain scope of the problem have not been strictly proved the correctness of Goldbach ' s conjecture, we still ho PE that can solve it.

If you find a even number of Goldbach conjectures is not true and then this question would be wrong, but we would like To congratulate the solving this math problem that have plagued humanity for hundreds of years.

Input:

The first line of input is a T means the number of the cases.

Next T lines, each of which is a postive even integer n (2<n<2^63).

Output:

The output is a also T lines, each of which is a number we asked for.

T is about 100.

The answer is not the only one, the answer is correct

Sample Input
18
Sample Output
3 5
Solving: Preprocessing 1e6-range Prime, violence these prime numbers AI uses primes to determine whether N-ai is a prime number
Pre-treatment: template for linear sieve (this is the first template I've just started collecting.)
#include <bits/stdc++.h>using namespacestd;Const intmaxn=1e6+7;BOOLCHECK[MAXN];intPRIME[MAXN];intMain () {inti,j,pos=0, Flag;  for(i=2; i<maxn;i++){        if(!check[i]) prime[pos++]=i;  for(j=0; j<pos&&i*prime[j]<maxn;j++) {Check[i*prime[j]]=true; if(i%prime[j]==0) Break; }    }
View Code

And then the prime judge really ignorant, the prime judge really does not, count so big

Check out someone else's blog:

51867240

Https://www.cnblogs.com/SinGuLaRiTy2001/p/6591414.html

First, a few theoretical foundations:

1. Fermat theorem: If P is a prime number, A is an integer, and a, p coprime, then A (p-1) divided by the remainder of P is constant equal to 1, namely: a^ (p-1) ≡1 (mod p).

But the reverse is not necessarily true, that is, if a, p coprime, and a^ (p-1) ≡1 (mod p), can not be introduced p is prime, such as Carmichael number.

2. Two-time detection theorem: If P is a prime number, 0<x<p, then the solution of the equation x^2≡1 (mod p) is x=1 or x=p-1.

3. Rules for modulo operation: (a*b)%n= (a%n * b%n)%n

4. Rapid accumulation of modulus, quick power to take the mold: You can look at a blog I wrote earlier simple and fast algorithm

These theoretical foundations have not been well thought out.

Then the process of the algorithm:

For the number of N to be judged

1. First judge whether it is 2, yes, then return True.

2. The judgment is not less than 2, or composite, yes, the words will return false.

3. Make n-1=u*2^t, find out u,t, where u is odd.

4. Take a random a, and 1<a<n

/* According to Fermat theorem, if a^ (n-1) ≡1 (mod p) then n is very likely a prime number, if the equation is not set, it must not be a prime.

Because N-1=u*2^t, so a^ (n-1) =a^ (u*2^t) = (a^u) ^ (2^t). */

5. So we make x= (a^u)%n

6. Then the T-cycle, each cycle is y= (x*x)%n,x=y, so the T-cycle after x=a^ (u*2^t) =a^ (n-1)

7. Because the loop time y= (x*x)%n, and X is certainly less than N, just can use two times the detection theorem,

if (x^2)%n==1, that is, Y equals 1, if n is prime, then x==1| | X==n-1, if x!=1&&x!=n-1, then n is definitely not a prime number and returns False.

8. Run to this time x=a^ (n-1), according to Fermat theorem, x!=1 words, is definitely not prime, return false

9. Because the Miller-rabin obtained the correct rate of 75%, so many times to cycle step 4~8 to improve the correct rate

10. After the loop has not returned after many times, then N must be a prime number, return True

Here is the template:

#include <cstdlib>#include<ctime>#include<cstdio>using namespacestd;Const intCount=Ten;intModular_exp (intAintMintN) {    if(m==0)        return 1; if(m==1)        return(a%N); Long LongW=modular_exp (a,m/2, N); W=w*w%N; if(m&1) W=w*a%N; returnW;} BOOLMiller_rabin (intN) {    if(n==2)        return true;  for(intI=0; i<count;i++)    {        intA=rand ()% (n2)+2; if(Modular_exp (a,n,n)! =a)return false; }    return true;}intMain () {Srand (Time (NULL)); intN; scanf ("%d",&N); if(Miller_rabin (n)) printf ("Probably a prime."); Elseprintf ("A Composite."); printf ("\ n"); return 0;}
View Code

Miller-rabin Prime number detection algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.