C ++: Miller-Rabin prime number (prime number) Detection Algorithm

Source: Internet
Author: User
2.1 theoretical basis of Miller-RABIN

Fermat TheoremNIs a odd prime number,AIs any positive integer (1 ≤AN? 1), thenAN? 1 pmodN. [2]

Theoretical Basis of Miller-Rabin AlgorithmNIs an odd prime numberN? 1 indicates 2S*RForm,RIs odd,AAndNIs any integer of the vegetarian, thenARLimit 1 pmodNOrJ(0 ≤JS? 1,JεZ) EquationA2JRWhy? 1 pmodNYes. [2]

This theory is derived from the Fermat theorem:NIs an odd prime number, then the equationX2 then 1 pmodNThere is only one or two solutions.

Theorem 3X,YAndNIs an integer. IfX2 =Y2 pmodN,X==±YPmodN, Then (X?Y) AndNThere are non-trivial factors in N.

2.2 Miller-Rabin Algorithm Description

Input: an odd integer n greater than 3 and a security parameter t greater than or equal to 1 (used to determine the number of test rounds ).

Output: returns whether N is a prime number (in the probability sense, the probability of misjudgment is generally less than (1/2) 80 ).

  1. Express n-1 as 2SR

  2. Perform the following operations on I from 1 to T:

    1. Select a random integer a (2 ≤AN? 2)

    2. ComputingYBytesARBmodN

    3. IfYAndY=N? 1. perform the following operations cyclically. Otherwise, go to 3:

      1. JLimit 1

      2. WhenJS? 1 andY=N? 1. perform the following operations in a loop; otherwise, jump to (iv .)

      3. ComputingYBytesY2 bmodN, IfY= 1 returns the "sum". OtherwiseJBytesJ+ 1

      4. IfY=N? 1 returns the "sum"

  3. Returns the "prime number"

Here, step (VI.) is obtained based on Theorem 3. [2]

2.3 probability of misjudgment of Miller-Rabin Algorithm

After the independent T-round Miller-Rabin algorithm, the probability of a combination being misjudged as a prime number is no greater than 4?T. This probability is the best algorithm given the Fermat theorem. This misjudgment probability is proved by the two theorem below.

Theorem 1: SetD=GCD(K,M) Then in the Finite Group {G1,G+ 2 ,···,GMIn = 1} (G is the generative element of a finite group, and m is the order of a finite group), d Elements satisfy the equation.XK= 1.

Theorem 2: SetPIs a singular number,P? 1 = 2SH(HIs an odd number), then in the multiplication group (Z/PZ) * Satisfies the equationX2RT=? 1 pmodP(TThe number of elements is: 0, ifRS; 2RGCD(H,T) IfR<S.

Using these two theorems, we will discuss the input N of the algorithm in three cases:

  1. N is the time when it can be divisible by the square of a prime number;

  2. N is the product of two different prime numbers;

  3. N is the product of two or more different prime numbers.

In this way, we can prove the upper limit of false judgment probability of the miiler-Rabin algorithm.

2.4 Miller-Rabin algorithm time complexity

Miller-Rabin is a probability algorithm. The calculation of the algorithm is concentrated in the cycles of STEP (B) and step (c). The worst case is (IV .) the cycle is not introduced in the middle, the worst case of a round of Miller-Rabin algorithm complexity is (1 +O(1) log2 (N). If the time complexity is measured by Single-precision multiplication, the worst-case time complexity of the Miller-Rabin algorithm is O (log23 (N)). In terms of time complexity, the performance of the Miller-Rabin algorithm is good. In practical applications, the miller-Rabin algorithm is executed quickly.

Code implementation:

Click (here) to fold or open

  1. # Include <iostream>
  2. Using namespace STD;
  3. Typedef unsigned _ int64 llong;
  4. Llong mod_pro (llong X, llong y, llong N)
  5. {
  6. Llong ret = 0, TMP = x % N;
  7. While (y)
  8. {
  9. If (Y & 0x1) if (Ret + = TMP)> N) ret-= N;
  10. If (TMP <= 1)> N) TMP-= N;
  11. Y> = 1;
  12. }
  13. Return ret;
  14. }
  15. Llong Mod (llong A, llong B, llong C)
  16. {
  17. Llong ret = 1;
  18. While (B)
  19. {
  20. If (B & 0x1) ret = mod_pro (Ret, a, c );
  21. A = mod_pro (a, a, c );
  22. B> = 1;
  23. }
  24. Return ret;
  25. }
  26. Llong ran ()
  27. {
  28. Llong ret = rand ();
  29. Return ret * rand ();
  30. }
  31. Bool is_prime (llong N, int T)
  32. {
  33. If (n <2) return false;
  34. If (n = 2) return true;
  35. If (! (N & 0x1) return false;
  36. Llong K = 0, M, A, I;
  37. For (M = n-1 ;! (M & 1); m >>= 1, K ++ );
  38. While (t --)
  39. {
  40. A = Mod (RAN () % (n-2) + 2, m, n );
  41. If (! = 1)
  42. {
  43. For (I = 0; I <K &! = N-1; I ++)
  44. A = mod_pro (a, a, n );
  45. If (I> = k) return false;
  46. }
  47. }
  48. Return true;
  49. }
  50. Int main ()
  51. {
  52. Llong N;
  53. While (scanf ("% i64u", & N )! = EOF)
  54. If (is_prime (n, 3 ))
  55. Cout <"Yes \ n ";
  56. Else
  57. Cout <"NO \ n ";
  58. Return 0;
  59. }
Related Article

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.