"C" calculates the greatest common divisor and least common multiple of two integers

Source: Internet
Author: User
Tags greatest common divisor

Background record of mathematical knowledge:

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

Calculate gcd (x, y)/function int get_gcd (int x,int y) as above thought;

In fact, 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 number of the first to find a convention is the request.

1 intGET_GCD (intXinty)2 {3     inttemp;4     inti;5     if(x>y)6     {7temp=x;8x=y;9y=temp;Ten     } One     if(y%x==0) A         returnx; -      for(i=x/2;i>1; i--) -         if(x%i==0) the             if(y%i==0) -                 returni;  -     return 1; - } +  - intGET_LCM (intXinty) + { A  return(x*y)/(GET_GCD (x, y)); at}

"C" calculates the 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.