Miller Robin prime number test and Pollcard Lyapunov factorization

Source: Internet
Author: User

<1> preparation algorithm [cpp] LL mul (LL a, LL B, LL p) {LL rn = 0, I; for (I = 1; I <= B; I <= 1, a = (a + a) % p) if (B & I) rn = (rn + a) % p; return rn ;} // product of two big numbers LL ksm (LL a, LL B, LL p) {LL rn = 1; for (; B; a = mul (, p), B> = 1) if (B & 1) rn = mul (rn, a, p); return rn ;} // In the computing model, the two large numbers multiply by LL gcd (LL a, LL B) {LL tmp; if (a <B) tmp = a, a = B, B = tmp; while (B) tmp = a % B, a = B, B = tmp; return a;} // calculate the maximum approximate number <2> Miller Robin prime number test can be in (0.25) ^ S determines the prime number at the error rate, which must be paid accordingly The time complexity of output (S * log n. The principle is based on two theorems: 1. If p is a prime number, 0 <a <p, then a ^ (p-1) 1_1 (mod p ). 2. For 0 <x <p, x ^ 2 then 1 (mod p), there are only two solutions: x = 1 and x = p-1. [Cpp] bool isprime (LL n) {if (n = 2) return true; if (n <2 |! (N & 1) return false; LL a, x, y, u = n-1; int t = 0; while (u & 1) = 0) t ++, u> = 1; for (I = 0; I <S; I ++) {a = rand () % (n-1) + 1; x = ksm (, u, n); for (int j = 1; j <= t; j ++) {y = mul (x, x, n ); if (y = 1 & x! = 1 & x! = N-1) return false; x = y;} if (x! = 1) return false;} return true;} <3> the complexity of Pollcard rock factor decomposition seems to be n ^ (1/4), which should be the fastest Algorithm for factorization. [Cpp] void ROV (LL n) {if (isprime (n) {list [++ top] = n; return;} LL a, x, y, z, p; while (true) {for (x = 1, y = 1, z = rand () + 1, p = 1; p = 1 ;) {www.2cto.com y = (mul (y, y, n) + z) % n; p = gcd (x-y + n) % n, n ); x = (mul (x, x, n) + z) % n; y = (mul (y, y, n) + z) % n ;} if (p = n) continue; Arg (p); Arg (n/p); return ;}} is the correctness clear, and complexity analysis-in short, it is very fast.

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.