由于最大公约数和最小公倍数满足:
lcm = m*n/gcd
lcm*gcd = m*n
则:
public class Main {
public static void main(String[] args) {
System.out.println(gcd(4, 8));
System.out.println(lcm(5, 5));
}
//gcd最大公约数
public static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
//lcm最小公倍数
public static long lcm(long a, long b) {
if (a != 0 || b != 0) {
return a * b / gcd(a, b);
} else {//0和任何数的最小公倍数都是0
return 0;
}
}
}