Find the lowest common multiple of two integers
- 1. C implementation to find the least common multiple of two integers and detailed code explanation
- 2. C++ implementation to find the least common multiple of two integers and detailed code explanation
- 3. Java implementation of finding the least common multiple of two integers and detailed code explanation
The least common multiple of two integers is the smallest of their common multiples. To require the lowest common multiple, the following principle can be used:
-
Decompose the prime factors: Decompose the two integers into prime factors and write them in the form of products of prime numbers.
-
Find the common factor: Find the common factor among the prime factors of each of the two integers and multiply them.
-
Take out the non-common factors: Multiply the remaining prime factors.
-
Multiply: Multiply the results obtained in steps 2 and 3 to obtain the least common multiple.
For example, find the least common multiple of 6 and 12:
6 = 2 × 3
12 = 2 × 2 × 3
Step 1: Factor 6 and 12 into prime factors to get 6 = 2 × 3, 12 = 2 × 2 × 3.
Step 2: Their common factors are 2 and 3.
Step 3: The non-common divisor of 6 is 3 and the non-common divisor of 12 is 2 × 2.
Step 4: Multiply the results obtained in steps 2 and 3, that is, 2 × 3 × 2 × 2 = 24.
Therefore, the least common multiple of 6 and 12 is 24.
1. C implementation to find the least common multiple of two integers and detailed code explanation
The least common multiple (LCM) is the smallest common multiple of two or more integers.
For example, the least common multiple of 4 and 6 is 12, because 12 is a multiple of both 4 and 6, and there is no number smaller than 12 that is a multiple of both 4 and 6.
Code:
#include <stdio.h>
//函数声明
int gcd(int a, int b);
int lcm(int a, int b);
int main()
{
int a, b;
printf("请输入两个正整数:");
scanf("%d %d", &a, &b);
printf("最小公倍数为:%d\n", lcm(a, b));
return 0;
}
//求最大公约数
int gcd(int a, int b){
if(b == 0)
return a;
return gcd(b, a % b);
}
//求最小公倍数
int lcm(int a, int b){
return a * b / gcd(a, b);
}
Detailed code explanation:
- In the main function, input the required two positive integers and call the
lcm
function to output the least common multiple. gcd
The function is used to find the greatest common divisor of two numbers, and will be called in thelcm
function.lcm
The function calculates the least common multiple using the formulaa * b / gcd(a, b)
by finding the greatest common divisor of two numbers, and returns the result.
Results of the:
请输入两个正整数:12 18
最小公倍数为:36
2. C++ implementation to find the least common multiple of two integers and detailed code explanation
The least common multiple (LCM) is the smallest common multiple of two or more integers. The following is a C++ code to find the least common multiple of two integers.
#include <iostream>
using namespace std;
int gcd(int a, int b) //辗转相除法求最大公约数
{
return (b == 0) ? a : gcd(b, a % b);
}
int lcm(int a, int b) //最小公倍数
{
return a * b / gcd(a, b);
}
int main()
{
int a, b;
cout << "请输入两个正整数:";
cin >> a >> b;
cout << "它们的最小公倍数为:" << lcm(a, b) << endl;
return 0;
}
Among them, the gcd()
function uses the euclidean division method to find the greatest common divisor, and the lcm()
function directly finds the least common multiple based on the formula. In the main()
function, input two positive integers from the console, and then call the lcm()
function to output their least common multiple.
Detailed code explanation:
gcd()
Function: The parameters are two positive integers a, b. If b is 0, then gcd(a,b) is a; otherwise gcd(a,b) is equal to gcd(b,a%b).lcm()
Function: The parameters are two positive integers a, b. First find the greatest common divisor gcd of a, b, and then use the formula lcm = a * b / gcd to calculate the least common multiple.main()
function: input two positive integers a, b from the console. Call thelcm()
function to calculate the least common multiple and output it to the console.
The above is the C++ code implementation for finding the least common multiple of two integers.
3. Java implementation of finding the least common multiple of two integers and detailed code explanation
The least common multiple is the product of two numbers divided by their greatest common divisor, so we need to first implement the function to find the greatest common divisor, and then use it to find the least common multiple.
code show as below:
public class LCM {
// 求最大公约数
public static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
// 求最小公倍数
public static int lcm(int a, int b) {
return a * b / gcd(a, b);
}
public static void main(String[] args) {
int a = 12;
int b = 18;
int res = lcm(a, b);
System.out.println("最小公倍数为:" + res);
}
}
First we define a function to find the greatest common divisorgcd
, which uses a recursive method to continuously modulo larger numbers into smaller numbers until the remainder is 0, the smaller number is the greatest common divisor.
Then we define a function to find the least common multiplelcm
, which directly calls the gcd
function to find the greatest common divisor, and then divides it by the product of two numbers Find the least common multiple using the greatest common divisor.
Finally, in the main
function we give two integers a
and b
and call lcm
The function finds their least common multiple and outputs the result.