Hey guys, I'm trying to solve a problem I received in school. It states:

Write a method named checkFermat that takes four integers as parameters—a, b, c
and n—and that checks to see if Fermat’s theorem (a^n + b^n = c^n) holds. If n is greater than 2 and it turns out to be true that a^n + b^n = c^n, the program should print “Holy smokes,Fermat was wrong!” Otherwise the program should print “No, that doesn’t work.”

You should assume that there is a method named raiseToPow that takes two integers
as arguments and that raises the first argument to the power of the second.

I'm a little stumped on how I can use my method raiseToPow to help me solve checkFermat. Can someone explain to me how I might incorporate the two methods to find my answer? Right now I believe I can only calculate a + b = c. My code so far is below:

package fermat;

/**
 *
 * @author Josh
 */
public class Main {

    public static void checkFermat (int a, int b, int c, int n) {
    if (n <= 2) {
        System.out.println ("Error: Enter a number larger than two");
        return;
        } else if (a + b == c ) {
            System.out.println ("Holy smokes,Fermat was wrong!");
        } else {
            System.out.println ("Fermat was right!");
        }}


    public static int raiseToPow (int x, int n) {
        double xn;
        xn = Math.pow((double)x, (double)n);
        return (int)xn;
    }
  
    public static void main(String[] args) {
        // TODO code application logic here
      checkFermat (3, 3, 3, 3);
      raiseToPow (3, 3);
    }

}

Recommended Answers

All 10 Replies

deleted

Deleted

Could you add comments to your code explaining what the code is supposed to do?
For example what is this for:

} else if (a + b == c ) {

It's suppose to test whether a + b = c and if so, print "fermer was right". But I want it so that a^n + b^n = c^n, but I think I'm suppose to use raiseToPow for that. That's where I am confused.

It's suppose to test whether a + b = c and if so, print "fermer was right"

The code you have does what it's supposed to do.
Does it work correctly or not?

I don't receive any errors, but its difficult to tell with the theorem given. I can't tell whether raiseToPower changes a + b == c into a^n + b^n == c^n. I just don't understand how it correlates to checkFermat and this is why I'm unsure of my code.

Before you can write a program you need to understand what the program is supposed to do. You need to research the theorem that your code is supposed to verify so you understand what your code needs to do.
Then you can write the code.

I did not mean it like that. I understand the theorem, I do not understand how the two methods interact in main.

What I meant when I said it was difficult to tell by the theorem was that there is nothing that agrees with a + b = c or a^n + b^n = c^n when n must be greater than two. But I guess I could try removing (n > 2) from the code to test it...

I'm sorry, the theorem in the question is stated a bit weird. I'm trying to prove that a^n + b^n DOES NOT equal c^n

I'm lost. If you understand the theorem and say that: It's suppose to test whether a + b = c
To me it looks like your code is not related to the theorem.
Instead of adding the values of the variables and comparing their sum to a third variable, doesn't the theorem say to sum the variables raised to a power and compare that to the third variable raised to the same power?

I guess you need to take small steps.
Write a program using your method or the Math.pow method to raise 4 to the power of 4 and print out the results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.