Use the C language to achieve the maximum public multiples of multiple numbers

Source: Internet
Author: User

[Cpp]
# Include <stdio. h>
 
// Obtain the minimum public multiple
Int doLCM (int * array, int size ){
Int x, y, temp, gcd = array [0], I, result = 1;
For (I = 0; (I + 1) <size; I ++ ){
X = gcd;
Y = array [I + 1];
// Ensure that x> y
If (x <y ){
Temp = y;
Y = x;
X = temp;
}
Gcd = getGCD (x, y );
}

// Divide each number by the maximum common number, and then multiply
For (I = 0; I <size; I ++ ){
Result * = array [I]/gcd;
}
// Multiply the last value by the maximum common number
Return result * gcd;
}
 
// Obtain the maximum number of public approx.
Int getGCD (int a, int B ){
If (a % B = 0 ){
Return B;
} Else {
Return getGCD (B, a % B );
}
}
 
Int main (void ){
Int array [3] = {144,24, 36 };
Int result = doLCM (array, 3 );
Printf ("% d \ n", result );
Return 0;
}

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.