Data structure and algorithm--greatest common divisor, least common multiple

Source: Internet
Author: User
Tags gcd greatest common divisor

1, two number for least common multiple: the idea is that two numbers, they are about to greatest common divisor, the remaining two numbers should be coprime, and their product is the least common multiple of these two numbers.

 PackageCom.lfy;ImportJava.util.Scanner;/** *  * @authorLfy *@since2018/07/03 *@version1.0*/ Public classLeastcommonmultiple { Public Static voidMain (string[] args) {@SuppressWarnings ("Resource") Scanner Input=NewScanner (system.in); intnum1=Input.nextint (); intNum2=Input.nextint (); //least common multiple for two numbersSystem.out.println (Get_leastcommonmultiple (num1,num2)); }    //least common multiple    Private Static intGet_leastcommonmultiple (intNUM1,intnum2) {                returnnum1*num2/get_greatestcommondivisor (NUM1,NUM2); }    //Greatest Common Divisor    Private Static intGet_greatestcommondivisor (intNUM1,intnum2) {        intRes=0; if(num1<num2) {NUM1=num1+num2; Num2=num1-num2; NUM1=num1-num2; }        if(num1%num2==0) {res=num2; }         while(num1%num2>0) {NUM1=num1%num2; if(num2>NUM1) {NUM1=num1+num2; Num2=num1-num2; NUM1=num1-num2; }            if(num1%num2==0) {res=num2; }        }        returnRes; }}

2, for two number of greatest common divisor, the main starting thought is based on the two-digit greatest common divisor nature:

Property 1 if A>b, then A and B are the same as the greatest common divisor of a-B, i.e. GCD (A, b) = GCD (A, B)
Property 2 If B>a, then A and B are the same as the greatest common divisor of a and b-a, i.e. GCD (A, b) = GCD (A, b-a)
Property 3 If A=b, the greatest common divisor of A and B are the same as the A and B values, i.e. gcd (A, b) = a = b

Data structure and algorithm--greatest common divisor, least common multiple

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.