如何在不使用Java的reduce方法的情况下获得相同的结果?

huangapple go评论110阅读模式
英文:

How to get same result without using Java's reduce method?

问题

我有一个方法,应该能够计算多个数字的最小公倍数(LCM)。它使用了 Java 的 reduce() 方法,因此数字 1、2、3 会得到正确的最小公倍数 6:

  1. int lcmAnswer = Arrays.stream(numbers).reduce(1, (a, b) -> {
  2. int total = lcmm(a, b);
  3. return total;
  4. }
  5. );
  6. System.out.println(lcmAnswer); // LCM(1, 2, 3) = 6

然而,如果不使用 Java 的 reduce() 方法,那么数字 1、2、3 就无法得到最小公倍数 6。而是得到了错误的最小公倍数 LCM(1, 2, 3) = 8:

  1. int[] numbers = {1, 2, 3};
  2. System.out.println(lcmm(1, 2, 3)); // LCM(1, 2, 3) = 8,这是错误的
  3. private static int lcmm(int... numbers) {
  4. int sum = 0;
  5. for (int i = 0; i < numbers.length - 1; i++) {
  6. int curr = numbers[i];
  7. int next = numbers[i + 1];
  8. sum += lcm(curr, next);
  9. }
  10. return sum;
  11. }
  12. private static int lcm(int p, int q) {
  13. // 返回最小公倍数
  14. return p * q / gcd(p, q);
  15. }
  16. private static int gcd(int p, int q) {
  17. // 使用欧几里得算法返回最大公约数
  18. int temp;
  19. while (q != 0) {
  20. temp = q;
  21. q = p % q;
  22. p = temp;
  23. }
  24. return p;
  25. }

有人知道我做错了什么吗?

英文:

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:

  1. int lcmAnswer = Arrays.stream(numbers).reduce(1, (a, b) -&gt; {
  2. int total = lcmm(a, b);
  3. return total;
  4. }
  5. );
  6. 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:

  1. int[] numbers = {1, 2, 3};
  2. System.out.println(lcmm(1,2,3)); // LCM(1, 2, 3) = 8, which is wrong
  3. private static int lcmm(int... numbers) {
  4. int sum = 0;
  5. for (int i = 0; i&lt;numbers.length -1; i++) {
  6. int curr = numbers[i];
  7. int next = numbers [i+1];
  8. sum += lcm(curr, next);
  9. }
  10. return sum;
  11. }
  12. private static int lcm(int p, int q) {
  13. // Return lowest common multiple.
  14. return p * q / gcd(p, q);
  15. }
  16. private static int gcd(int p, int q) {
  17. //Return greatest common divisor using Euclid&#39;s Algorithm.
  18. int temp;
  19. while (q != 0) {
  20. temp = q;
  21. q = p % q;
  22. p = temp;
  23. }
  24. return p;
  25. }

Does someone has any idea what I'm doing wrong?

答案1

得分: 1

假设我们有四个数字 a,b,c,d。要计算 a,b,c,d 的最小公倍数(LCM),需要按照以下步骤进行:

设最终的 LCM 为 RES

  • 计算前两个数字 ab 的 LCM。将值赋给 RES。因此,RES = LCM(a,b)
  • 计算 RESc 的 LCM。更新 RES 的值。因此,RES = LCM(RES,c)
  • 计算 RESd 的 LCM。更新 RES 的值。因此,RES = LCM(RES,d)

最终的 RES 值将包含 a,b,c,d 的 LCM。

我们可以按照这个算法计算多个数字的 LCM。

以下是实现代码:

  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. class LCMMultiple{
  5. public static void main (String[] args) throws java.lang.Exception{
  6. int[] numbers = {1, 2, 3};
  7. System.out.println(getLcmMultiple(numbers));
  8. }
  9. private static int getLcmMultiple(int... numbers) {
  10. int lcm = 0;
  11. for (int i = 0; i<numbers.length -1; i++) {
  12. int curr = numbers[i];
  13. int next = numbers [i+1];
  14. if(lcm != 0){
  15. lcm = getLcm(lcm, getLcm(curr, next));
  16. }
  17. else{
  18. lcm = getLcm(curr, next);
  19. }
  20. }
  21. return lcm;
  22. }
  23. private static int getLcm(int p, int q) {
  24. // 返回最小公倍数。
  25. return p * q / getGcd(p, q);
  26. }
  27. private static int getGcd(int p, int q) {
  28. // 使用欧几里德算法返回最大公约数。
  29. int temp;
  30. while (q != 0) {
  31. temp = q;
  32. q = p % q;
  33. p = temp;
  34. }
  35. return p;
  36. }
  37. }

输出结果:

  1. 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 and b. Assign the value to RES. So, RES = LCM(a,b).
  • Calculate LCM of RES and c. Update the value of RES. So, RES = LCM(RES,c).
  • Calculate LCM of RES and d. Update the value of RES. 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:

  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. class LCMMultiple{
  5. public static void main (String[] args) throws java.lang.Exception{
  6. int[] numbers = {1, 2, 3};
  7. System.out.println(getLcmMultiple(numbers));
  8. }
  9. private static int getLcmMultiple(int... numbers) {
  10. int lcm = 0;
  11. for (int i = 0; i&lt;numbers.length -1; i++) {
  12. int curr = numbers[i];
  13. int next = numbers [i+1];
  14. if(lcm != 0){
  15. lcm = getLcm(lcm, getLcm(curr, next));
  16. }
  17. else{
  18. lcm = getLcm(curr, next);
  19. }
  20. }
  21. return lcm;
  22. }
  23. private static int getLcm(int p, int q) {
  24. // Return lowest common multiple.
  25. return p * q / getGcd(p, q);
  26. }
  27. private static int getGcd(int p, int q) {
  28. //Return greatest common divisor using Euclid&#39;s Algorithm.
  29. int temp;
  30. while (q != 0) {
  31. temp = q;
  32. q = p % q;
  33. p = temp;
  34. }
  35. return p;
  36. }
  37. }

Output:

  1. 6

huangapple
  • 本文由 发表于 2020年8月18日 04:54:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63458507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定