C # seek LCM

Source: Internet
Author: User
Tags gcd

The

LCM can be gcd by dividing the product of two numbers by these two numbers. For example, the GCD of 12 and 9 is 3, then the two-digit LCM is calculated as 12*9/3, and the result of the calculation is LCM of the two numbers. The code is as follows:

 private int Getzxgbs (params int[] parameters) 
{
if (parameters. Length = = 1)
return parameters[0];
List<int> resultlist=new list<int> ();
if (parameters. Length% 2 = 0)
{
for (int i = 0; i < parameters. Length; i=i+2)
{
int result=mingongbeishu (parameters[i],parameters[i+1]);
Resultlist.add (Result);
}
}
Else
{
for (int i = 0; i < parameters. Length; i = i + 2
{
int result = Mingongbeishu (Parameters[i], parameters[i + 1]);
Resultlist.add (Result);
}
Resultlist.add (parameters[parameters. Length-1]);
}
Return Getzxgbs (Resultlist.toarray ());
}
public int Mingongbeishu (int n1, int n2)
{
Int temp = Math.max (n1, N2);
N2 = Math.min (n1, N2); in//n2, the smallest of the two numbers in
N1 = Temp;//n1 hold the largest
int product = N1 * n2;//to find the product of two numbers
while (N2 != 0
{
N1 = n1 > n2? n1:n2;//to make the number in N1 greater than the number in n2
int m = n1% N2;
N1 = n2;
N2 = m;
}
return (PRODUCT/N1);//LCM
}
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.