Miller_robin Determination of large prime numbers

Source: Internet
Author: User

According to acdreamer's blog http://blog.csdn.net/acdreamers/article/details/7913786

On 51nod, I saw a prime number in the range of 10 ^ 30. It is definitely not good to create a table. We should use Miller-Robin to determine the addition of the biginteger in Java.

First, based on the Fermat small theorem, when gcd (A, P) = 1, a ^ (PM) % P = 1. Therefore, after a random number of rand (2, P-1) is generated, if the input is a prime number, then there must be a POW (p-1) % P = 1. Here we use the Fast Power modulo. After a probe, if the result is not 1, P must not be a prime number; otherwise, 75% of the probability is a prime number, so there are 10 probes

1-(0.75) ^ 10 = the probability of 0.94369 is a prime number (as if so ). Of course, you can find 20 more times, with a success rate of 0.996828.

 

There is also an optimization here, that is, according to the second probe theorem described in the above blog x ^ 2 = 1 (mod P), when P is a prime number, X must be 1 or P-1, then there is an optimization, first filtering out the two power of the P-1, assuming that the P-1 = m * 2 ^ sqr, first calculating x = POW (a, m) % P, then, check whether y = x * x % P = 1, but X is not 1, P-1 indicates that P is not a prime number.

The Code is as follows:

Import Java. io. *; import Java. util. *; import Java. math. biginteger; public class minller {public static final int iter = 10; public static biginteger fast_fac (biginteger A, biginteger N, biginteger mod) {biginteger two = biginteger. valueof (2), ans; A =. moD (MOD); ans = biginteger. one; while (! (N. equals (biginteger. zero) {If (n. moD (two )). equals (biginteger. one) {ans = (ans. multiply ()). moD (MOD); n = n. subtract (biginteger. one);} n = n. divide (two); A = (. multiply ()). moD (MOD);} return ans;} public static Boolean millerrobin (biginteger p) {biginteger two = biginteger. valueof (2), a, X, N, y = biginteger. zero, P_1; random RND = new random (); P_1 = P. subtract (biginteger. one); If (P. equals (biginteger. one) r Eturn false; If (P. equals (two) return true; If (P. moD (two )). equals (biginteger. zero) return false; int Max, sqr = 0; // filter out the power of 2 n = P. subtract (biginteger. one); While (n. moD (two )). equals (biginteger. zero) {++ sqr; n = n. divide (two);} If (N. compareto (biginteger. valueof (10000) = 1) max = 10000; else max = n. intvalue (); For (INT I = 0; I <ITER; ++ I) {A = biginteger. valueof (RND. nextint (MAX-2) + 2); X = fast_fac (A, N, P); If (X. equals (biginteger. one) continue; For (Int J = 0; j <sqr; ++ J) {Y = (X. multiply (x )). moD (p); If (Y. equals (biginteger. one )&&! X. Equals (biginteger. One )&&! X. equals (P_1) return false; X = y;} If (Y. equals (biginteger. one) = false) return false;} return true;} public static void main (string [] argv) {using CIN = new using (system. in); While (CIN. hasnextbiginteger () {biginteger n = cin. nextbiginteger (); If (millerrobin (N) system. out. println ("yes"); else system. out. println ("no ");}}}

Miller_robin Determination of large prime numbers

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.