Mathematics (1. Fermat theorem 2. Extended Euclidean algorithm 3. Momo inversion)

Source: Internet
Author: User
Tags gcd greatest common divisor

Fermat theorem (Fermat theory) is an important theorem in number theory: if P is prime, and gcd (a,p) = 1, then a(p-1) ≡1 (mod p). That is: If A is an integer, p is a prime number, and A,p coprime (that is, there is only one convention of 1), then the remainder of a (p-1) divided by P is constant equal to 1.

The extended Euclidean algorithm is used to solve a set of X, y in known a, b to satisfy the Bézout equation: ax+by = gcd (A, B) =d (the solution must exist, according to the correlation theorem in number theory). Extended Euclidean is commonly used in solving linear equations and equations.

Euclidean algorithm expands the Euclidean algorithm not only calculates (a, b) the greatest common divisor, but also can calculate the multiplication inverse of a-and B-mode A, in C language is described as follows:
1int gcd (intAintBint&ar,int&BR)2 {3     intx1,x2,x3;4     intY1,y2,y3;5     intT1,t2,t3;6     if(0==A)//There is a number of 0, there is no multiplication inverse7     {8Ar=0; br=0;9         returnb;Ten     } One     if(0==b) A     { -Ar=0; br=0; -         returnA; the     } -x1=1; x2=0; x3=A; -y1=0; y2=1; y3=b; -     intnk; +      for(t3=x3%y3;t3!=0; t3=x3%y3) -     { +k=x3/Y3; At2=x2-k*y2;t1=x1-k*Y1; atx1=y1;x2=y2;x3=Y3; -y1=t1;y2=t2;y3=T3; -     } -     if(y3==1)//There are multiplication inverse elements -     { -Ar=Y2; inBr=X1; -         return 1; to     } +     Else//number of Conventions not 1, non-multiplicative inverse -     { theAr=0; *Br=0; $         returnY3;Panax Notoginseng     } -}

000

Mathematics (1. Fermat theorem 2. Extended Euclidean algorithm 3. Momo inversion)

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.