0

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);
    }
}
kapex
  • 28,903
  • 6
  • 107
  • 121
Garrett Outlaw
  • 215
  • 3
  • 6
  • 16
  • 3
    Just a general word of advice, use more obvious variable names rather than just single letters. It can get complicated very quickly otherwise. – David Jan 29 '13 at 15:17
  • I won't read that code. For the reason stated above. – Adam Arold Jan 29 '13 at 15:18
  • Rather than `for (...) { t = y * x; } if (s == t) break;`, simply do `if (y % s == 0) break;`. – Bernhard Barker Jan 29 '13 at 15:20
  • Please don't omit a `for` loop condition then put an `if ... break` at the end of the loop block. That's confusing for those that follow and have to read/maintain the code. Ditto on the variable names. No, homework code won't be maintained, but it will be *read* by a grader, and it's better to get good habits started right away. – Alan Krueger Jan 29 '13 at 15:28
  • Check this: http://stackoverflow.com/questions/4201860/how-to-find-gcf-lcm-on-a-set-of-numbers – Pouya BCD Dec 18 '15 at 15:20

8 Answers8

2

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); 
}
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
2

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));
    }
} 
sanjaykumar81
  • 435
  • 1
  • 6
  • 13
1

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;
            }
        }
    }
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1
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;
}
VincBreaker
  • 178
  • 1
  • 9
G Dias
  • 89
  • 1
  • 3
0
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++;
    }



}
W00di
  • 954
  • 12
  • 21
0

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


}
Hitesh Kumar
  • 512
  • 1
  • 8
  • 27
0

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

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));
    }
Deepeshkumar
  • 395
  • 3
  • 13