user1940007
user1940007

Reputation: 751

Fermats equation in java

I am trying to make an recursive analysis program with fermat's last equation, but it keeps returning that the equation is wrong I don't understand?

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

Upvotes: 1

Views: 1224

Answers (2)

Peter
Peter

Reputation: 48978

The problem is that the powers you are using lead to 'Infinity' because the numbers are too big.

Since 'Infinity==Infinity' the program states that the line

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

is correct

Upvotes: 0

vikingsteve
vikingsteve

Reputation: 40408

Try removing the first line like this:

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

Ok, now see what Math.pow(100000,100000) gives (Infinity). I think the problem is that you are using values that are too high (at least for n).

Upvotes: 1

Related Questions