[英]I am facing an error in the following java code can anyone help me out
class Solution {
long maxGcd(int N) {
long a = (N * (N - 1))/ gcd(N,N-1);
long b = ((N-2)*(N-3))/ gcd(N-2,N-3);
long c = (a*b)/ gcd(a,b);
long num = N*(N-1);
int count = 0;
for (int i = N - 2; i >= 1; i--) {
if (gcd(num, i) == 1) {
num *= i;
count++;
}
if (count == 2) break;
}
return Math.max(c, num);
}
long gcd(long x, long y)
{
if(y==0)
return x;
return gcd(y,x*y);
}
}
Error:错误:
Exception in thread "main" java.lang.ArithmeticException: / by zero
线程“main”中的异常 java.lang.ArithmeticException: / 为零
I think I am getting this error because if, at some point, b=0 then c will be undefined.我想我收到这个错误是因为如果在某个时候 b=0 那么 c 将是未定义的。 I tried changing it to
我试着把它改成
if(b!=0){
long c =(a*b)/gcd(a,b)
but after running it, gives an error saying但是运行之后报错
prog.java:43: error: cannot find symbol
return Math.max(c, num);
Can anyone help me with the solution of this error?谁能帮我解决这个错误?
Given an integer N. Find maximum LCM (Least Common Multiple) that can be obtained from four numbers less than or equal to N.给定一个整数 N。找到可以从四个小于或等于 N 的数字中获得的最大 LCM(最小公倍数)。
Note: Duplicate numbers can be used.注意:可以使用重复的数字。
Example 1:示例 1:
Input:输入:
N = 4 N = 4
Output: 12输出:12
Explanation:解释:
The four numbers can be [4,4,3,2] or [4,4,4,3], etc. It can be shown that 12 is the maximum LCM of four numbers that can be obtained from numbers less than or equal to 4.四个数可以是[4,4,3,2]或[4,4,4,3]等。可以证明12是小于等于数可得到的四个数的最大LCM到 4。
I have tried the above problem but it was giving me unexpected error.我已经尝试过上述问题,但它给了我意想不到的错误。
If I'm getting this right, it looks like there is a problem with your function "gcd": the recursive loop will never end如果我做对了,看起来你的函数“gcd”有问题:递归循环永远不会结束
If you divide some digit by 0 it will throw an error known as: java.lang.ArithmeticException如果你将某个数字除以 0,它会抛出一个错误,称为: java.lang.ArithmeticException
Solution
解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.