Programming US $2.7 Max. Common, least common, 2.7 common

Source: Internet
Author: User

Programming US $2.7 Max. Common, least common, 2.7 common

The question in the book is to find the maximum common divisor of two numbers. In fact, when we learned the C language, the algorithm that the teacher talked about was a type problem with the prime number in teaching.

The method we learned at that time was "moving phase division", that is, using the formula f (x, y) = f (y, x % y) until x % y = 0, x is the maximum common divisor of two numbers.

However, the book says that multiplication and division operations are a waste of time. Therefore, we can think about this problem in another way. multiplication and division cannot be used, so we can only add or subtract values, in the book, if a number can divide x and y at the same time, it must be able to divide x-y and y at the same time. Therefore, we define the maximum common divisor of x and y as f (x, y ), then, based on the above idea, we can get f (x, y) = f (x-y, y ).

The function declaration is as follows:

int DutTheGreatestCommonDivisor(int, int);

The source code is as follows:

/** The idea here is: if a number can divide x and y at the same time, it must be able to divide x-y and y at the same time, therefore, * If we define the maximum common divisor of x and y as f (x, y), we can obtain f (x, y) = f (x-y, y) */int DutTheGreatestCommonDivisor (int m, int n) {/* When m is less than n, two numbers need to be exchanged */if (m <n) return DutTheGreatestCommonDivisor (n, m);/* WHEN n is equal to 0, the return m is the maximum public approx. */if (n = 0) return m; elsereturn DutTheGreatestCommonDivisor (m-n, n );}

Now that we have talked about the maximum common number, it corresponds to the minimum common number. When we were in the last few years, we learned a formula. The calculation method of the minimum common number of two numbers is: (x * y)/maximum common divisor (x, y ).

The function declaration is as follows:

int DutTheLeastCommonMultiple(int, int);

The source code is as follows:

/** The least common multiple can be obtained by using the maximum common approx. Mathematical derivation can look at this: * http://baike.baidu.com/view/341375.htm */int DutTheLeastCommonMultiple (int x, int y) {return (x * y) /DutTheGreatestCommonDivisor (x, y );}




Minimum Public multiples and maximum public approx.

Minimum Public multiple
The public multiples of several numbers are called the Public multiples of these numbers, and the smallest one is called the minimum public multiples of these numbers.
Minimum Public multiple:
Square brackets are commonly used in mathematics. For example, [, 20] is the minimum public multiple of 12, 18, and 20.

Method of least common multiple:

There are two methods to calculate the least common multiple of several natural numbers:
(1) decomposition prime factor method. First, we break down these numbers into prime factors, and then combine all their public prime factors with several public prime factors and the unique prime factors of each number, the product is the least common multiple of them.
For example, for [, 20], because 12 = 2 ^ 2 × 3, 18 = 2 × 3 ^ = 2 ^ 2 × 5, the public prime factor of three numbers is 2, the public prime factor of two numbers is 2 and 3, and the prime factor of each number is 5 and 3. Therefore, [12, 18, 20] = 2 ^ 2 × 3 ^ 2 × 5 = 180. (Short Division calculation is available)

(2) Public method. Because the product of the two numbers is equal to the product of the maximum and least common multiples of the two numbers. That is, (a, B) × [a, B] = a × B. Therefore, to obtain the least common multiples of two numbers, you can first find their maximum common divisor, and then use the above formula to find their least common multiples.
For example, if [180] is used, [] = 18 × 20 then () = 18 × 20 then 2 =. Calculate the least common multiples of several natural numbers. You can first find the least common multiples of two numbers, and then find the least common multiples of the least common multiple and the third number, and find them in sequence until the last one. The minimum public multiple obtained at the end is the minimum public multiple of the desired number.

Maximum common approx.
It refers to the largest of the several integers in total.

For example, the common values of 12 and 30 are: 1, 2, 3, and 6, of which 6 is the most common values of 12 and 30.

Programming of java least common multiples and maximum Common Divisor

Import java. util. collections;

Public class CommonDivisorAndCommonMultiple
{
// The following method is used to obtain the maximum public approx.
Public static int gcd (int m, int n)
{
While (true)
{
If (m = m % n) = 0)
Return n;
If (n = n % m) = 0)
Return m;
}
}

Public static void main (String args []) throws Exception
{
// Obtain the input value
Cin = new partition (System. in );
Int a = cin. nextInt (), B = cin. nextInt ();
Int c = gcd (a, B );
System. out. println ("least common multiple:" + a * B/c + "\ n maximum common approx.:" + c );
}
}
Well, this is purely a mathematical algorithm, and I don't think I can comment it out.

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.