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