GCD – Greatest common divisor LCM – Least common multiple
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class Algorithms { public static long getGcd(long a, long b) { if (a == 0) return b; if (b == 0) return a; do { long r = a % b; a = b; b = r; } while (b != 0); return a; } public static long getLcm(long a, long b) { long gcd = getGcd(a, b); if (gcd != 0) return Math.abs(a * b) / gcd; else return 0; } } |