Finding the LCM of given n numbers.

Posted on 2006-03-21
Last Modified: 2012-06-27
Any one can write the code for finding the LCM of given n numbers.

I have written the code for finding the LCM of 2 numbers, but I need to find the LCM of given n numbers.

int gcd(int a, int b)
      if (a==0 || b==0) return max(a,b);
      if (a > b)       return gcd(b, a);
      if (!(b % a)) return a;
      return gcd(a, b % a);

int lcm(int a, int b)
   return b*a/gcd(a,b);

Question by:picasothakkar
    LVL 22

    Expert Comment

    Just create a lcm_n function that recursively calls itself or delegates to lcm.

    If lcm_n is called with only one number, return that number.
    If lcm_n is called with more than one number, then have it call itself recursively.
        a = lcm_n of the first half of the numbers
        b = lcm_n of the second half of the numbers
        return lcm(a,b)
    LVL 22

    Expert Comment

    p.s. this sounds suspiciously like homework, thus no code.
    LVL 2

    Expert Comment

    wouldn't it be

    lcm(a1,...,aN) = lcm(a1,lcm(a2,...aN)) ???
    LVL 9

    Accepted Solution

    I think you can optimize GCDs code to be like:
    //assume that a and b cannot both be 0
    int GCD(int a, int b)
       if (b==0) return a;
       return GCD(b,a%b);
    >>but I need to find the LCM of given n numbers
    so, the numbers should be stored in array, right?
    then you can do it using for loop :
    int array[N];
    int output = array[0];    
    for(int i = 1; i<N; i++)
         output = lcm(output,array[i]);
    or you can do it recursively as  experts said ..

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now