Calculates the greatest common divisor and least common multiple of two integers

Source: Internet
Author: User
Tags greatest common divisor

Algorithm One

Any >1 integer can be written in the form of one or more prime factor products, and the prime product factor appears in non-descending order.

Then integer x, y can be labeled as:
X=P1x1p2x2... pmxm

Y=p1y1p2y2... pmym

(Where P1,p2,.... Is the prime number, if the exponent of the necessary prime factor XJ or YJ can be 0)

(1) Greatest common divisor gcd (x, y) =p1min (x1,y1)p2min (x2,y2)... pmmin (xm,ym)

(2) least common multiple LCM (x, y) =p1max (x1,y1)p2max (x2,y2)... pmmax (XM,YM)

(3) It is also possible to obtain: LCM (x, y) *gcd (x, y) =x*y

It takes at least two steps to calculate gcd (x, y) according to the idea

Step1:decompose_to_primes (int n);//divide integer n into prime number multiplication form

STEP2:GET_GCD (int x,int y);//Calculate GCD (x, y) according to the formula (1) based on the results of STEP1

The ratio of the obvious computational volume is large.

In fact, from a programmatic point of view, the value of x, Y is not very large. If the simple calculation of greatest common divisor and least common multiple can not be so complex, can be from large to small traverse min (x, y) of the approximate number of the first to find a convention is the request.

intGET_GCD (intXinty) {    inttemp; inti; if(x>y) {temp=x; X=y; Y=temp; }    if(y%x==0)        returnx;  for(i=x/2;i>1; i--)        if(x%i==0)            if(y%i==0)                returni; return 1;}intGET_LCM (intXinty) { return(x*y)/(GET_GCD (x, y));}

Algorithm two

Using the Euclid algorithm (i.e., the Euclidean method)

Step1, make R A/b derived remainder (0≤r<b). If r= 0, the algorithm ends, then B is the desired, otherwise execute step2.

Step2, A←b,b←r, re-execute step1.
intgcdintXintY//Euclid Method{    intR; if(x<y) {R=x; X=y; Y=R; } R=x%y;  while(r) {x=y; Y=R; R=x%y; }        returny;}

Forward from http://www.cnblogs.com/wxiaoli/p/5335419.html

Computes greatest common divisor and least common multiple of two integers

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.