Title Description:
Interface description
Prototype:
Long Getmaxdivisor (long lfirstinput, long lsecondinput);
Input parameters:
int First: integer number one;
int second: second integer;
return value:
Greatest common divisor
Long Getminmultiple (long lfirstinput, long lsecondinput);
Input parameters:
int First: integer number one;
int second: second integer;
return value:
Least common multiple
Problem-solving ideas: Using the greatest common divisor method can be obtained, two number of least common multiple = two number product/two number of greatest common divisor
Euclidean method:
... R1 (0≤R1). If the r1=0, then (A, B) =b, if r1≠0, then the second divided by the R1, B÷r1=q ... r2 (0≤R2). If r2=0, then (A, B) =r1, if r2≠0, continue to use R1 in addition to R2, ... So go on until you can divide it evenly. The divisor of the last remainder of the dividend is (a, b). For example: A=25,b=15,a/b=1 ... 10,b/10=1 ...... 5,10/5=2 ....... 0, the last one is the divisor of the remainder of the divisor is 5,5 is to seek greatest common divisor. The code is as follows:
public static long Getmaxdivisor (long lfirstinput, long Lsecondinput) { long mul,temp; Mul=lfirstinput*lsecondinput; while (lfirstinput!=0) {temp=lfirstinput;lfirstinput=lsecondinput%lfirstinput;lsecondinput=temp;} return lsecondinput; } function: Get two integers of least common multiple //input: Two integers //return: least common multiple public static long getminmultiple (long lfirstinput, long Lsecondinput) { long mul,temp; Mul=lfirstinput*lsecondinput; while (lfirstinput!=0) {temp=lfirstinput;lfirstinput=lsecondinput%lfirstinput;lsecondinput=temp;} return mul/lsecondinput; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
2015 Machine Test--Calculate the greatest common divisor and least common multiple of two positive integers