Least Common Multiple
Asked Answered
T

8

26

I have the current coding which used to be a goto but I was told to not use goto anymore as it is frowned upon. I am having troubles changing it into for say a while loop. I am fairly new to C# and programming in general so some of this is completely new stuff to me. Any help would be appreciated. The actual question is input two numbers and find the lowest common multiple.

Here is the original with goto:

BOB:
    if (b < d)
    {                
        a++;
        myInt = myInt * a;
        b = myInt;
        myInt = myInt / a;

        if (b % myInt2 == 0)
        {
            Console.Write("{0} ", h);
            Console.ReadLine();
        }

    }
    if (d < b)
    {
        c++;
        myInt2 = myInt2 * c;
        d = myInt2;
        myInt2 = myInt2 / c;

        if (d % myInt == 0)
        {
            Console.Write("{0} ", t);
            Console.ReadLine();
        }
        else
        {
            goto BOB;
        }

    }
    else
    {
        goto BOB;
    }

   }
Tila answered 26/11, 2012 at 17:16 Comment(4)
What if d == b? Also it would be much easier if you showed us the original code that includes goto. Otherwise, a quick google for "c# least common multiple` might show some useful results...Mcgruder
More importantly... what is the question?Marcus
If this is homework, you might want to talk to your TA.Lympho
@Lympho It is an independent study class, nobody to ask just papers telling me what to do.Tila
C
13

Try This:

using System;

public class FindLCM
{
    public static int determineLCM(int a, int b)
    {
        int num1, num2;
        if (a > b)
        {
            num1 = a; num2 = b;
        }
        else
        {
            num1 = b; num2 = a;
        }

        for (int i = 1; i < num2; i++)
        {
            int mult = num1 * i;
            if (mult % num2 == 0)
            {
                return mult;
            }
        }
        return num1 * num2;
    }

    public static void Main(String[] args)
    {
        int n1, n2;

        Console.WriteLine("Enter 2 numbers to find LCM");

        n1 = int.Parse(Console.ReadLine());
        n2 = int.Parse(Console.ReadLine());

        int result = determineLCM(n1, n2);

        Console.WriteLine("LCM of {0} and {1} is {2}",n1,n2,result);
        Console.Read();
    }
}

Output:

Enter 2 numbers to find LCM
8
12
LCM of 8 and 12 is 24
Canebrake answered 26/11, 2012 at 17:20 Comment(1)
Thank you but I would like to learn what I am doing and not just take from someone else.Tila
S
52

Here's a more efficient and concise implementation of the Least Common Multiple calculation which takes advantage of its relationship with the Greatest Common Factor (aka Greatest Common Divisor). This Greatest Common Factor function uses Euclid's Algorithm which is more efficient than the solutions offered by user1211929 or Tilak.

C#, C++, C:

static int gcf(int a, int b)
{
    while (b != 0)
    {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

static int lcm(int a, int b)
{
    return (a / gcf(a, b)) * b;
}

For more information see the Wikipedia articles on computing LCM and GCF.

Schaffel answered 29/12, 2013 at 11:57 Comment(1)
For more than two numbers, see https://mcmap.net/q/135633/-least-common-multiple-for-3-or-more-numbersTherrien
C
13

Try This:

using System;

public class FindLCM
{
    public static int determineLCM(int a, int b)
    {
        int num1, num2;
        if (a > b)
        {
            num1 = a; num2 = b;
        }
        else
        {
            num1 = b; num2 = a;
        }

        for (int i = 1; i < num2; i++)
        {
            int mult = num1 * i;
            if (mult % num2 == 0)
            {
                return mult;
            }
        }
        return num1 * num2;
    }

    public static void Main(String[] args)
    {
        int n1, n2;

        Console.WriteLine("Enter 2 numbers to find LCM");

        n1 = int.Parse(Console.ReadLine());
        n2 = int.Parse(Console.ReadLine());

        int result = determineLCM(n1, n2);

        Console.WriteLine("LCM of {0} and {1} is {2}",n1,n2,result);
        Console.Read();
    }
}

Output:

Enter 2 numbers to find LCM
8
12
LCM of 8 and 12 is 24
Canebrake answered 26/11, 2012 at 17:20 Comment(1)
Thank you but I would like to learn what I am doing and not just take from someone else.Tila
I
6

Hey if anyone need something more modern :)

public static class MathHelpers
{
    public static T GreatestCommonDivisor<T>(T a, T b) where T : INumber<T>
    {
        while (b != T.Zero)
        {
            var temp = b;
            b = a % b;
            a = temp;
        }

        return a;
    }

    public static T LeastCommonMultiple<T>(T a, T b) where T : INumber<T>
        => a / GreatestCommonDivisor(a, b) * b;
    
    public static T LeastCommonMultiple<T>(this IEnumerable<T> values) where T : INumber<T>
        => values.Aggregate(LeastCommonMultiple);
}
Iosep answered 11/12, 2022 at 22:43 Comment(0)
H
1

Try this

 int number1 = 20;
 int number2 = 30;
 for (tempNum = 1; ; tempNum++)
 {
   if (tempNum % number1 == 0 && tempNum % number2 == 0)
   {
       Console.WriteLine("L.C.M is - ");
       Console.WriteLine(tempNum.ToString());
       Console.Read();
       break;
    }
 }

// output -> L.C.M is - 60
Haematosis answered 26/11, 2012 at 17:27 Comment(0)
N
0

Here is one recursive solution. It might be on some interview question. I hope it helps

    public static int GetLowestDenominator(int a, int b, int c = 2)
    { 
        if (a == 1 | b == 1) {
            return 1;
        }
        else if (a % c == 0 & b % c == 0)
        {
            return c;
        }
        else if (c < a & c < b)
        {
            c += 1;
            return GetLowestDenominator(a, b, c);
        }
        else
        {
            return 0;
        }
    }
Nobel answered 4/2, 2015 at 7:52 Comment(0)
T
0
        int num1, num2, mull = 1;

        num1 = int.Parse(Console.ReadLine());  
        num2 = int.Parse(Console.ReadLine());

        for (int i = 1; i <= num1; i++)
        {
            for (int j = 1; j <= num2; j++)
            {
                if (num1 * j == num2 * i)
                {
                    mull = num2 * i;
                    Console.Write(mull); 
                    return;
                }
            }
        }
Theosophy answered 26/1, 2016 at 12:29 Comment(0)
M
0

Here is much optimized solution for finding LCM.

 private static int lcmOfNumbers(int num1, int num2)
    {
        int temp = num1 > num2 ? num1 : num2;
        int counter = 1;
        while (!((temp* counter++) % num1 == 0 && (temp* counter++) % num2 == 0)) {
        }
        return temp* (counter-2);
    }
Montiel answered 26/10, 2017 at 11:26 Comment(1)
Hey there. I get the impression that by "much optimized" you mean the while hack, but could you explain how and why that is actually faster than easier to read code? I was under the general impression that compilers are generally much better than humans at optimizing code.Vipul
B
0
int n1 = 13;
int n2 = 26;

for (int i = 2; i <= n1; i++)
 {
    if (n1 % i == 0 && n2 % i == 0)
    {
    Console.WriteLine("{0} is the LCM of {1} and 
    {2}",i,n1,n2);
    break;
 }

  }
Billman answered 21/3, 2019 at 18:40 Comment(1)
Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Blatman

© 2022 - 2024 — McMap. All rights reserved.