Miller_rabin large prime number test and pollard_rov integer decomposition Template

Source: Internet
Author: User

tag: Io AR for html sp amp on Ad c

# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> using namespace STD; typedef _ int64 ll; const int times = 20; ll factor [100], L; ll gcd (ll a, LL B) {return B? Gcd (B, A % B): A;} ll add_mod (ll a, LL B, ll N) {ll ans = 0; while (B) {If (B & 1) ans = (ANS + a) % N; B >>= 1; A = (a <1) % N;} return ans ;} ll pow_mod (ll a, ll M, ll N) {ll ans = 1; while (m) {If (M & 1) ans = add_mod (ANS, A, N ); m> = 1; A = add_mod (a, a, n);} return ans;} bool witness (ll a, ll N) {Int J = 0; ll M = n-1; while (! (M & 1) {J ++; m >>= 1 ;} ll x = pow_mod (a, m, n ); if (x = 1 | x = N-1) return true; while (j --) {x = add_mod (x, x, n); If (x = N-1) return true;} return false;} bool miller_rabin (ll n) {If (n <2) return false; If (n = 2) return true; If (! (N & 1) return false; For (INT I = 0; I <times; I ++) {ll A = rand () % (n-1) + 1; if (! Witness (A, n) return false;} return true;} ll pollard_rov (ll n, ll c) {ll I = 1, x = rand () % (n-1) + 1, y = x, K = 2, D; // srand (Time (null); While (true) {I ++; X = (add_mod (X, x, n) + C) % N; D = gcd (Y-X, n); If (D> 1 & D <n) return D; if (y = x) return N; if (I = k) {Y = x; k <= 1 ;}} void get_fact (LL N, ll K) {If (n = 1) return; If (miller_rabin (N) {factor [L ++] = N; return;} ll p = N; while (P> = N) {P = pollard_rov (P, K --);} get_fact (P, K); get_fact (N/P, K );}

Miller_rabin large prime number test and pollard_rov integer decomposition Template

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.