-1

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sharpzain120
  • 365
  • 3
  • 9
  • 18

5 Answers5

1

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++;
    }
  }
}
Akshay
  • 227
  • 2
  • 4
1

**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;
    }
}
1

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

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    His method is actually [Prime Factorization](https://secure.wikimedia.org/wikipedia/en/wiki/Least_common_multiple#Finding_least_common_multiples_by_prime_factorization). But yeah, it works. – MSalters Nov 01 '11 at 15:16
1

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    Isn't the LCM of 4 and 8 actually 8, not 4? And GCD(8,4) yields 4, so (8*4)/4 = 8, so the formula in the question generates the correct answer? The LCM has to be at least as big as the larger of the two input values (assuming all positive numbers). – Jonathan Leffler Nov 01 '11 at 18:24
  • Eh, yes, fixed. The GCD and LCM methods are quite closely related. If you factor both terms in prime factors, take the respective minimums, and multiply them, you get the GCD (here: min(2,3)=2, 2x2=4). Take the respctive maximums (here: max(2,3)=3, 2x2x2=8) and you get the LCM. – MSalters Nov 03 '11 at 10:24
0

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;
    }

}
Vpn_talent
  • 1,290
  • 12
  • 21