英文:
How to get same result without using Java's reduce method?
问题
我有一个方法,应该能够计算多个数字的最小公倍数(LCM)。它使用了 Java 的 reduce() 方法,因此数字 1、2、3 会得到正确的最小公倍数 6:
int lcmAnswer = Arrays.stream(numbers).reduce(1, (a, b) -> {
int total = lcmm(a, b);
return total;
}
);
System.out.println(lcmAnswer); // LCM(1, 2, 3) = 6
然而,如果不使用 Java 的 reduce() 方法,那么数字 1、2、3 就无法得到最小公倍数 6。而是得到了错误的最小公倍数 LCM(1, 2, 3) = 8:
int[] numbers = {1, 2, 3};
System.out.println(lcmm(1, 2, 3)); // LCM(1, 2, 3) = 8,这是错误的
private static int lcmm(int... numbers) {
int sum = 0;
for (int i = 0; i < numbers.length - 1; i++) {
int curr = numbers[i];
int next = numbers[i + 1];
sum += lcm(curr, next);
}
return sum;
}
private static int lcm(int p, int q) {
// 返回最小公倍数
return p * q / gcd(p, q);
}
private static int gcd(int p, int q) {
// 使用欧几里得算法返回最大公约数
int temp;
while (q != 0) {
temp = q;
q = p % q;
p = temp;
}
return p;
}
有人知道我做错了什么吗?
英文:
I have method which should give the LCM of multiple numbers. It works with Java's reduce() method so the numbers 1,2,3 gives the LCM of 6, which is correct:
int lcmAnswer = Arrays.stream(numbers).reduce(1, (a, b) -> {
int total = lcmm(a, b);
return total;
}
);
System.out.println(lcmAnswer); // LCM(1, 2, 3) = 6
However if I don't use Java's reduce() method then the numbers 1,2,3 don't give me the LCM of 6. It gives me LCM(1, 2, 3) = 8, which is wrong:
int[] numbers = {1, 2, 3};
System.out.println(lcmm(1,2,3)); // LCM(1, 2, 3) = 8, which is wrong
private static int lcmm(int... numbers) {
int sum = 0;
for (int i = 0; i<numbers.length -1; i++) {
int curr = numbers[i];
int next = numbers [i+1];
sum += lcm(curr, next);
}
return sum;
}
private static int lcm(int p, int q) {
// Return lowest common multiple.
return p * q / gcd(p, q);
}
private static int gcd(int p, int q) {
//Return greatest common divisor using Euclid's Algorithm.
int temp;
while (q != 0) {
temp = q;
q = p % q;
p = temp;
}
return p;
}
Does someone has any idea what I'm doing wrong?
答案1
得分: 1
假设我们有四个数字 a,b,c,d
。要计算 a,b,c,d
的最小公倍数(LCM),需要按照以下步骤进行:
设最终的 LCM 为 RES
。
- 计算前两个数字
a
和b
的 LCM。将值赋给RES
。因此,RES = LCM(a,b)
。 - 计算
RES
和c
的 LCM。更新RES
的值。因此,RES = LCM(RES,c)
。 - 计算
RES
和d
的 LCM。更新RES
的值。因此,RES = LCM(RES,d)
。
最终的 RES
值将包含 a,b,c,d
的 LCM。
我们可以按照这个算法计算多个数字的 LCM。
以下是实现代码:
import java.util.*;
import java.lang.*;
import java.io.*;
class LCMMultiple{
public static void main (String[] args) throws java.lang.Exception{
int[] numbers = {1, 2, 3};
System.out.println(getLcmMultiple(numbers));
}
private static int getLcmMultiple(int... numbers) {
int lcm = 0;
for (int i = 0; i<numbers.length -1; i++) {
int curr = numbers[i];
int next = numbers [i+1];
if(lcm != 0){
lcm = getLcm(lcm, getLcm(curr, next));
}
else{
lcm = getLcm(curr, next);
}
}
return lcm;
}
private static int getLcm(int p, int q) {
// 返回最小公倍数。
return p * q / getGcd(p, q);
}
private static int getGcd(int p, int q) {
// 使用欧几里德算法返回最大公约数。
int temp;
while (q != 0) {
temp = q;
q = p % q;
p = temp;
}
return p;
}
}
输出结果:
6
英文:
Suppose we have four numbers a,b,c,d
. To calculate LCM of a,b,c,d
, we need to follow these steps:
Let the final LCM is RES
.
- Calculate LCM of first two numbers
a
andb
. Assign the value toRES
. So,RES = LCM(a,b)
. - Calculate LCM of
RES
andc
. Update the value ofRES
. So,RES = LCM(RES,c)
. - Calculate LCM of
RES
andd
. Update the value ofRES
. So,RES = LCM(RES,d)
.
The final value of RES
will contain the LCM of a,b,c,d
.
We can follow this algorithm to calculate LCM of multiple numbers.
Here is the implementation:
import java.util.*;
import java.lang.*;
import java.io.*;
class LCMMultiple{
public static void main (String[] args) throws java.lang.Exception{
int[] numbers = {1, 2, 3};
System.out.println(getLcmMultiple(numbers));
}
private static int getLcmMultiple(int... numbers) {
int lcm = 0;
for (int i = 0; i<numbers.length -1; i++) {
int curr = numbers[i];
int next = numbers [i+1];
if(lcm != 0){
lcm = getLcm(lcm, getLcm(curr, next));
}
else{
lcm = getLcm(curr, next);
}
}
return lcm;
}
private static int getLcm(int p, int q) {
// Return lowest common multiple.
return p * q / getGcd(p, q);
}
private static int getGcd(int p, int q) {
//Return greatest common divisor using Euclid's Algorithm.
int temp;
while (q != 0) {
temp = q;
q = p % q;
p = temp;
}
return p;
}
}
Output:
6
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论