最大公約數(Greatest Common Divisor, GCD)
最小公倍數(Least Common Multiple, LCM)
#include <iostream>
// 函式:計算兩個數的最大公約數(GCD),這被稱為歐幾里得演算法
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// 函式:計算兩個數的最小公倍數(LCM)
int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
int main() {
int num1, num2;
std::cout << "請輸入兩個整數: ";
std::cin >> num1 >> num2;
std::cout << "最大公約數是: " << gcd(num1, num2) << std::endl;
std::cout << "最小公倍數是: " << lcm(num1, num2) << std::endl;
return 0;
}