least common multiplier

This is a discussion on least common multiplier within the C Programming forums, part of the General Programming Boards category; I've ever writen a function returning `greatest common divisor'. But now I need `least common multiplier' to carry on my ...

  1. #1
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187

    least common multiplier

    I've ever writen a function returning `greatest common divisor'.
    But now I need `least common multiplier' to carry on my project.
    Please anyone suggest me its concept. In order that I can write it by myself.

    Any help would be much appreciated.
    Thanks,
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Are you having problems with the maths or the code?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    I have problem with math.
    I only need its concept.
    I want to write it by myself.
    Sorry in late reply.

    Thanks,
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Last edited by Dave_Sinkula; 03-14-2003 at 10:25 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    32
    To work out by brute force:

    Start looping at the lowest of the two given numbers multiplied by two. Loop every number up to the product of the two numbers.
    In the loop, if you modulus the current tested number by the first number and the second number, and both are 0, return the current tested number.
    After the loop return the product of the two numbers.
    - Tigs

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Split the number into prime numbers. Then, for each prime number, pick the one with the most amount and multiply that to the answer.
    If that sounded too weird (I guess it did) then look at this:

    1500 = 2 * 2 * 3 * 5 * 5 * 5
    126 = 2 * 3 * 3 * 7

    For 2:s, the first one have 2 and the second has 1. 2 > 1 so we pick 2.

    For 3:s, the first one has 1 and the second has 2. 1 < 2 so we pick 2.

    For 5:s, the first one has 3 and the second one has 0. 3 > 0 so we pick 3.

    For 7:s, the first one has 0 and the second one has 1. 0 < 1 so we pick 1.

    The least common multiplier is:
    2^2 * 3^2 * 5^3 * 7^1 = 31500

    test:
    31500 = 1500 * 21
    31500 = 126 * 250

    21 = 7 * 3
    neither 7 nor 3 splits 250

    31500 is the least common multiplier!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Thanks all of you. That is what I need.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. common functions
    By FoodDude in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 02:13 PM
  3. Binary traslator
    By NiVaG in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2005, 07:01 PM
  4. Lowest Common Factor
    By PJYelton in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2002, 08:30 AM
  5. Using AVI's from dll in animation common control
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 01-16-2002, 12:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21