Find the least common multiple and greatest common divisor of two numbers
This afternoon the first arithmetic lesson to stay small job, do it smoothly.
The greatest common divisor and least common multiple of the concept is not explained, Violence Act does not say undesirable.
Said here Euclidean and Decreases method .
Euclidean
Euclidean algorithm, is a method for seeking the greatest common divisor of two natural numbers, also known Euclidean algorithm.
Assuming m> = n, the formula: gcd (m, n) = gcd (n, m mod n)
means that the greatest common divisor of n and m = n and m / n of a remainder, until m mod n = 0, then n is the greatest common divisor, and the least common multiple = (m n) / GCD (m, n)
Note: in the code usually written as m / gcd (m, n) * n, prevented m n excessively large burst of data may explode the scope of
the Java code
public static int gcd(int m, int n) {
return m % n == 0 ? n : gcd(n, m % n);
}
public static void main(String[] args) {
//最大公约数 5
System.out.println(gcd(25, 15));
//最小公倍数 75
System.out.println(25 / gcd(25, 15) * 15);
}
Decreases law
Formula: gcd (m, n) = gcd (n, mn), until mn = 0, then m or n is the smallest divisor
input two integers m and n (m> = n):
1) If mn = 0, then m (or n) is the greatest common divisor of two numbers
2) If mn ≠ 0, then m = n, n = mn and then go back to Step 1
public static int gcd2(int m, int n) {
return m - n == 0 ? n : gcd2(n, m - n);
}
public static void main(String[] args) {
//最大公约数 5
System.out.println(gcd2(25, 15));
//最小公倍数 75
System.out.println(25 / gcd2(25, 15) * 15);
}
Consecutive integers detection algorithm
The overall strategy is to start with the smallest start addition, if you are depleted in return, will not divide into -1 cycle.
public static int gcd3(int m, int n) {
int gcd = 1;
if (m == n) {
return n;
} else {
int min = m > n ? n : m;
for (int i = min; i > 1; i--) {
if (n % i == 0 && m % i == 0) {
return i;
}
}
return gcd;
}
}
public static void main(String[] args) {
//连续整数检测
//最大公约数 5
System.out.println(gcd3(25, 15));
//最小公倍数 75
System.out.println(25 / gcd3(25, 15) * 15);
}
Decomposition quality factor then find all common divisor Quadrature
m, n each prime factor decomposition, and then compare the two groups to find all the common factor is the quadrature gcd
public static int gcd4(int m, int n) {
if (m == n) {
return n;
} else {
//m的因数
List<Integer> _first = new ArrayList<>();
//n的因数
List<Integer> _second = new ArrayList<>();
//m分解质因数
for (int i = 2; i <= m; i++) {
while (m % i == 0 && m != 0) {
m /= i;
_first.add(i);
}
if (m == i) {
_first.add(i);
break;
}
}
//n分解质因数
for (int i = 2; i <= n; i++) {
while (n % i == 0 && n != 0) {
n /= i;
_second.add(i);
}
if (n == i) {
_second.add(i);
break;
}
}
//克隆m的所有因数
Set<Integer> tmp = new HashSet<>(_first);
//在m因数中除去m因数与n因数中共有部分 即公因数
tmp.removeAll(_second);
//克隆m所有因数
Set<Integer> exist = new HashSet<>(_first);
//除去剩余的tmp,剩余exist即为所有公因数list
exist.removeAll(tmp);
int gcd = 1;
for (Integer i :exist) {
gcd*=i;
}
return gcd;
}
}
public static void main(String[] args) {
//公因数算法检测
//最大公约数 5
System.out.println(gcd4(25, 15));
//最小公倍数 75
System.out.println(25 / gcd4(25, 15) * 15);
}