How to find Least Common Multiple (LCM) for two numbers

7.4k Views Asked by At

I have used Euclid's method to find the L.C.M for two numbers.

l.c.m=a*b/(gcd(a,b))

How can I do this without using this algorithm? I have an idea of first getting all factors of these two numbers and storing them in array. Then take 1 element from array 1 and search for it in array2, if it present there then remove it from there and make the result multiply by that num.

Is this OK?

5

There are 5 best solutions below

2
On BEST ANSWER

Almost. What's the LCM of 4 and 8? Obviously 8 (23), but in your method you'd find 2. You need to keep track not just of all factors, but also how often they appear.

1
On

I believe the algorithm you suggest is a method using a table, check to see if it works for you.

0
On

**LCM of two numbers using while loop**

package whileloop1;

import java.util.Scanner;

public class Lcm {

public static void main(String[] args) {
    Lcm obj = new Lcm();
    obj.twoNumberLcm();
}
void twoNumberLcm       
{
    Scanner Sobj = new Scanner(System.in);
    System.out.println("Enter your First Number ");
    int num1 = Sobj.nextInt();
    System.out.println("Enter your Second Number ");
    int num2 = Sobj.nextInt();
    int big,small;
    if(num1>num2)
    {
        big = num1;
        small = num2;
    }
    else
    {
        big = num2;
        small = num1;
    }
    System.out.println(" Bigger number is " + big);
    System.out.println(" Smaller number is " + small);
    int smallcopy = small;
    int bigcopy = big;
    int count =1;
    while(count>0)
    {
        while(big>=small)
        {
            if(big == small)
            {
                System.out.println("Least common mutiples of given two numbers" + small);
                count--;
                break;
            }
            small=small+smallcopy;
        }
        big = big+bigcopy;
    }
}
0
On

You can get LCM of two number by getting GCD at first. Here is the solution for the above.

package com.practice.competitive.maths;

import java.util.Scanner;

public class LCMandGCD {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int testCases = scanner.nextInt();
            while (testCases-- > 0) {
                long number1 = scanner.nextInt();
                long number2 = scanner.nextInt();
                long gcd = computeGCD(number1, number2);
                long lcm = computeLCM(number1, number2, gcd);
                System.out.println(lcm + " " + gcd);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static long computeGCD(long number1, long number2) {
        while (number1 != number2) {
            if (number1 > number2)
                number1 -= number2;
            else
                number2 -= number1;
        }   
        return number2;
    }

    private static long computeLCM(long number1, long number2, long gcd) {
        return (number1*number2)/gcd;
    }

}
0
On

LCM(Least Common Multiple) is always greater than or equal to the larger of the two numbers. So we will first check that the larger number itself a LCM of two numbers by checking that the larger number is divisible by the smaller one , if yes we found the LCM & If no then we will increment the larger number by 1 and check again.

package com.company;
import java.util.Scanner;

public class Main {

public static void main(String args[]) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter the first Number : ");
    int number1 = scan.nextInt();
    System.out.print("Enter the second number : ");
    int number2 =scan.nextInt();

    int multiple;

    if(number1 >= number2) {
        multiple = number1;
    } else {
        multiple = number2;
    }

    Boolean loopContinue = true;

    while(loopContinue) {
        if(multiple % number1 == 0 && multiple % number2 == 0) {
            System.out.println("LCM of Two Numbers is " + multiple);
            loopContinue = false;
        }
        multiple++;
    }
  }
}