Java lcm lcm(long[] a)

Here you can find the source of lcm(long[] a)

Description

lcm

License

Open Source License

Declaration

public static long lcm(long[] a) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static long lcm(long a, long b) {
        return a * (b / gcd(a, b));
    }//from   w  w  w.j  av a2s  .co m

    public static long lcm(long[] a) {
        if (a.length == 1) {
            return a[0];
        } else {
            long lcm = 1;
            for (int i = 0; i < a.length; i++) {
                if (i == 0) {
                    lcm = lcm(a[i], a[i + 1]);
                } else {
                    lcm = lcm(a[i], lcm);
                }
            }
            return lcm;
        }
    }

    public static long gcd(long a, long b) {
        long r = b;
        while (b > 0) {
            r = b;
            b = a % b;
            a = r;
        }
        return r;
    }

    public static long gcd(long[] a) {
        if (a.length == 1) {
            return a[0];
        } else {
            long gcd = 1;
            for (int i = 0; i < a.length; i++) {
                if (i == 0) {
                    gcd = gcd(a[i], a[i + 1]);
                } else {
                    gcd = gcd(a[i], gcd);
                }
            }
            return gcd;
        }
    }
}

Related

  1. lcm(long a, long b)
  2. lcm(long a, long b)
  3. lcm(long a, long b)
  4. lcm(long u, long v)
  5. lcm(long x, long y)
  6. LCM1(int A, int B)
  7. lcmPositive(int... args)