我正在尝试使用fermat的最后一个等式制作一个递归分析程序,但它一直在返回,这个等式是错误的我不明白?
public class fermatdata
{
public static void data(int a, int b, int c, int n)
{
if ((Math.pow(a, n)) + (Math.pow(b, n)) == (Math.pow(c, n)))
{
System.out.println("Holy smokes, Fermat was wrong!");
return;
}
else
{
data(a-1, b-1, c-1, n-1);
if (n < 2)
{
return;
}
else
{
data(a-1, b-1, c-1, n-1);
}
}
System.out.println("No, that doesn't work. Fermat was right");
}
public static void main(String[] args)
{
System.out.println("Fermat's last theorem stated that the formula a^n + b^n = c^n while n>2 and all of the numbers are integers will never be correct. Here I am going to do an analysis with all the numbers starting at 100,000 and count backwords to 3");
data(100000, 100000, 100000, 100000);
}
}
答案 0 :(得分:1)
尝试删除第一行,如下所示:
data(a-1, b-1, c-1, n-1);
好的,现在看看Math.pow(100000,100000)给出了什么(Infinity)。我认为问题在于你使用的值太高(至少对于n)。
答案 1 :(得分:0)
问题是你使用的权力导致'无限',因为数字太大了。
由于'Infinity == Infinity',程序会声明该行
((Math.pow(a, n)) + (Math.pow(b, n)) == (Math.pow(c, n)))
是正确的