The maximum common divisor and the minimum public multipleAlgorithm
Today, the instructor assigned a homework question, asking Java for the maximum public approx. And the minimum public multiples. After completing this question, I thought about sorting out some information collected on the Internet and releasing it, for more people to learn.
MATERIALS:
(1) method for calculating the maximum common divisor:
Assume 3 and 11, first use 11% 3 = 2, then use 3% 2 = 12% 1 = 0
That is to say, the last 1 is the maximum common number, for example, 3 and 2222% 3 = 1; 3% 1 = 0; the algorithm obtained after analysis!
Euclid (Euclidean) Division
Int gcd (int A, int B)
{
If (B = 9) return;
Else return gcd (B, A % B );
}
(2) method for finding the maximum common divisor of multiple numbers
First, calculate the maximum common approx. E1 of the first two numbers, and then calculate the maximum common approx. Between E1 and the third number ....... You can find the maximum number of common approx.
(3) method for finding the least common multiple of two numbers
The product of the two values is the least common multiple except the maximum common divisor.
Question:
The design function gcd (a, B) represents the maximum common divisor of integers A and B. design method:
1. Calculate GCD (a, B) using loops and Recursion)
2. Calculate the maximum approximate number of three integers A, B, and C.
3. Request a, B
Minimum Public multiple
Source file:
Public class
Euclid
{
Public static void main (string ARGs [])
{
Int num1, num2;
Num1 = integer. parseint (ARGs [0]);
Num2 =
Integer. parseint (ARGs [1]);
System. Out. println ("the 1st number is" +
Num1 );
System. Out. println ("the 2nd number is" + num2 );
System. Out. println ("the result of Euclid is" + gcd (num1, num2 ));
}
Public static int gcd (int A, int B)
{
If (0
=)
{
Return B;
}
If (0 =
B)
{
Return;
}
If (A>
B)
{
Swap (A, B );
}
Int C;
For (C = A % B; C> 0; C = A % B)
{
A = B;
B = C;
}
Return
B;
}
Public static void swap (int A, int B)
{
Int c =;
A = B;
B = C;
}
}
The other two algorithms can be written based on the data.