1

フェルマーの最終定理を使って再帰分析プログラムを作成しようとしていますが、方程式が間違っていると返され続けます。わかりませんか?

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);   
    }   
}
4

2 に答える 2

1

次のように最初の行を削除してみてください。

        data(a-1, b-1, c-1, n-1);

では、Math.pow(100000,100000)が(Infinity)に何を与えるかを見てみましょう。問題は、(少なくともnに対して)高すぎる値を使用していることだと思います。

于 2013-02-24T19:45:50.203 に答える
0

問題は、数値が大きすぎるため、使用しているパワーが「無限大」につながることです。

'Infinity == Infinity'なので、プログラムは次のように述べています。

 ((Math.pow(a, n)) + (Math.pow(b, n)) == (Math.pow(c, n)))

正しい

于 2013-02-24T19:51:34.243 に答える