Java maximum common divisor and minimum common multiple

Source: Internet
Author: User

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
}

Related Article

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.