Java maximum common divisor and minimum common multiple
The maximum number of common appointments can be either of the following methods:
Division of moving phase:Also known as the Euclidean algorithm (Euclidean algorithm) is an algorithm used to calculate the maximum approximate number of two positive integers.
Moving and subtraction:That is, nikumanchesfa, which features a series of subtraction operations to obtain the maximum common number.
The following is the Java code:
Public class JavaBase
{
Static public int gcd1_1 (int x, int y) // Non-recursive moving phase division
{
Int temp;
Do {
Temp = x % y;
X = y;
Y = temp;
} While (temp! = 0 );
Return x;
}
Static public int gcd2_1 (int x, int y) // Non-recursive moving and Subtraction
{
Int max, min;
Int temp;
Max = (x> y )? X: y;
Min = (x <y )? X: y;
While (max! = Min)
{
Temp = max-min;
Max = (temp> min )? Temp: min;
Min = (temp <min )? Temp: min;
}
Return max;
}
Static public int gcd1_2 (int x, int y) // Recursive division of the Moving Phase
{
Return (y = 0 )? X: gcd1_2 (y, x % y );
}
Static public int gcd2_2 (int x, int y) // Recursive moving and Subtraction
{
If (x = y) return x;
Return (x> y )? Gcd2_2 (x-y, y): gcd2_2 (x, y-x );
}
Public static void main (String args [])
{
Int a = 28, B = 48;
Int g = 0;
G = gcd1_1 (a, B );
System. out. println ("maximum common Appointment:" + g );
G = gcd1_2 (a, B );
System. out. println ("maximum common Appointment:" + g );
G = gcd2_1 (a, B );
System. out. println ("maximum common Appointment:" + g );
G = gcd2_2 (a, B );
System. out. println ("maximum common Appointment:" + g );
System. out. println ("minimum public multiple:" + a * B/g );// Minimum public multiple
}