Garrett Outlaw
Garrett Outlaw

Reputation: 225

LCM (lowest common multiple) in Java

I need help with this loop. One of my course assignments is to make a LCM program.

Sample output:
(8,12) LCM is 24
(4,3) LCM is 12
(5,10,20) LCM is 20
(18,24,52) LCM is 936
(12,10,26) LCM is 780
(99,63,24) LCM is 5544
(62,16,24) LCM is 1488

I have this so far for 2 numbers but I'm not sure how to do 3 numbers. We're supposed to use methods on other classes so this is what I have for the LCM class.

public class LCM {
    private int n, x, s = 1, t = 1;

    public LCM()
    {
        n = 0;
        x = 0;
        s = 1;
        t = 1;
    }
    public int lcmFind(int i, int y) {
        for (n = 1;; n++) {
            s = i * n;
            for (x = 1; t < s; x++) {
                t = y * x;
            }
            if (s == t)
                break;
        }
        return (s);
    }
}

Upvotes: 0

Views: 33729

Answers (8)

Deepeshkumar
Deepeshkumar

Reputation: 443

You can re use the same function written for lcm of two numbers. Just pass one of the arguments as follows:

The function code can be like this:

public static int lcm(int num1,int num2) {
        boolean flag = false;
        int lcm = 0;
        for(int i= 1;!flag; i++){
           flag = (num1 < num2)?(num2*i)%num1==0:(num1*i)%num2==0;
           lcm = num1<num2?num2*i:num1*i;
        }
        return lcm;
    }

Call the function like this:

    public static void main(String[] args) {
        System.out.println("lcm "+lcm(lcm(20,80),40));
    }

Upvotes: 0

G Dias
G Dias

Reputation: 89

public static int gcd(int a, int b){
    return (b == 0) ? a : gcd(b, a % b);
}

public static int gcd(int... args){
    int r = args[0];
    int i = 0;
    while(i < args.length - 1)
        r = gcd(r,args[++i]);
    return r;
}

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

public static int lcm(int... args){
    int r = args[0];
    int i = 0;
    while(i < args.length - 1)
        r = lcm(r,args[++i]);
    return r;
}

Upvotes: 1

Try this

     int n1 = 72, n2 = 120, lcm;

    // maximum number between n1 and n2 is stored in lcm
    lcm = (n1 > n2) ? n1 : n2;

    // Always true
    while(true)
    {
        if( lcm % n1 == 0 && lcm % n2 == 0 )
        {
            System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
            break;
        }
        ++lcm;
    }

Upvotes: 0

sanjaykumar81
sanjaykumar81

Reputation: 445

I found this link and I guess this is most simple and clean solution:

/**
* Calculate Lowest Common Multiplier
*/
public static int LCM(int a, int b) {
    return (a * b) / GCF(a, b);
}

/**
* Calculate Greatest Common Factor
*/
public static int GCF(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return (GCF(b, a % b));
    }
} 

Upvotes: 3

Hitesh Kumar
Hitesh Kumar

Reputation: 514

I think you have the answer already, since it's an old post. still posting my answer. Below is the code to find the LCM for an array:

 import java.util.Arrays;
 import java.util.Scanner;

public class ArrayEqualAmz {
    static int lcm =1;
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] arr = new int[n];

    for(int i=0; i<n; i++){
        arr[i] = sc.nextInt();
    }
    System.out.println("lcm = "+lcm(arr));

    }

    // find the factor
    public static int divisor(int x[]){
        Arrays.sort(x);
        int num=0;
        for(int i=x.length-1; i>=0; i--){
            if(x[i] != 1 )
            num=x[i];
        }
        for(int j=2; j<=num; j++){
            if(num%j==0){
            return j;}
        }
        return num;
    }

    //finding the lcm
    public static int lcm(int arr[]){
        while(true){

        int j = divisor(arr);
        if(j==0){break;}
        lcm = lcm*j;
        for(int i=0; i<arr.length; i++){
            if(arr[i]%j==0){
            arr[i] = arr[i]/j;}
        System.out.print(arr[i]+",");
        }
        System.out.println( " factor= "+lcm);
        return lcm(arr);
        }
        return lcm;
    }


}

Upvotes: 0

Sabya
Sabya

Reputation: 968

static int  getLCM(int a,int b)
{
    int x;
    int y;
    if(a<b)
    {
        x=a;
         y=b;   
    }
    else
    {
         x=b;
         y=a;   
    }
    int i=1;
    while(true)
    {

        int x1=x*i;
        int y1=y*i;
        for(int j=1;j<=i;j++)
        {
        if(x1==y*j)
        {
            return x1;
        }
        }

        i++;
    }



}

Upvotes: 0

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

If you want to get LCM of 3+ numbers you can use your method lcmFind in following way:

int a = 2;
int b = 3;
int c = 5;
LCM l = new LCM();
int lcm = l.lcmFind(l.lcmFind(a, b), c);

Reccomendations:

  • Make n,x, s and t variables local in lcmFind. Because you need them ONLY in lcmFind method and you need to reset their values in every invocation of lcmFind.
  • Make your lcmFind method static. You don't need to instantiate new object in order to calc lcm. This way you can use it like LCM.lcmFind(3,4), or even better rename method and use something like LCM.find(3,4).

EDIT
If you need to make method that takes variable number of argument you should check varargs. So you'll get something like:

public int lcmFind(int.. args) {
    // args is actually array of ints.
    // calculate lcm of all values in array.
    // usage: lcmFind(1,4) or lcmFind(1,5,6,3)
}

You can use your first version of lcmFind that takes 2 arguments and calculate lcm of many values using it.

EDIT 2
If you need only 2 and 3-args version of lcmFind than you can just add 3-arg version:

public int lcmFind(int a, int b, int c) {
    return lcmFind(lcmFind(a, b), c); 
}

Upvotes: 2

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

try

public int lcm(int... a) {
    for (int m = 1;; m++) {
        int n = a.length;
        for (int i : a) {
            if (m % i != 0) {
                break;
            }
            if (--n == 0) {
                return m;
            }
        }
    }
}

Upvotes: 1

Related Questions