Algorithm for maximum common divisor and least common multiple

Source: Internet
Author: User

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.

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.