小编码挑战(费马的最后定理)

时间:2016-12-15 20:15:57

标签: java

我正在努力学习Java;这是我正在努力的练习:

  

费马的最后定理说没有整数a,b和c,除非在n = 2的情况下,否则a ^ n + b ^ n = c ^ n。

     

编写一个名为checkFermat的方法,该方法将四个整数作为参数-a,b,c和n-并检查Fermat定理是否成立。如果n大于2并且结果证明a ^ n + b ^ n = c ^ n,程序应该打印“神圣的烟雾,费马错了!”否则程序应该打印“不,那不是”工作。“

     

你应该假设有一个名为raiseToPow的方法,它将两个整数作为参数,并将第一个参数提升为第二个参数的幂。例如:   int x = raiseToPow(2, 3);   会将值8赋值给x,因为2 ^ 3 = 8。

我遇到了一些问题,例如我似乎无法将Math.Pow(a, n)int一起使用,只能使用double。如果您有兴趣,这就是我到目前为止的内容,请随意跳过它,并在答案中编写您自己的程序版本。

(请记住,我几天前才开始写这本书。)

    package fermat.s_last_theorem;
    import java.lang.Math;
    import java.util.Scanner;

    public class FermatS_Last_Theorem {


    public static void main(String[] args) {

    Scanner s = new Scanner (System.in);
    System.out.println("Inster First Number");
    double frst = s.nextDouble();
    System.out.println("Insert Second Number");
    double scnd = s.nextDouble();

    System.out.println("Insert Exponent");
    double expo = s.nextDouble();


    double v =  FLaw(frst,scnd,expo);
    double k = FLawRes(v, expo);

    System.out.println("The answer is " + v);
    System.out.println("Your answer rooted by your exponent is " + k);
    Law(v, Pow(k, expo));


    }

    public static double Pow(double a, double b) {
    double res = Math.pow (a, b);
    return (res);
    }

    public static double FLaw(double frst, double scnd, double expo) {
    double D1 = Pow(frst, expo);
    double D2 = Pow(scnd, expo);


    return (D1 + D2);

     }

     public static double FLawRes(double res, double base) {

     double D3 = Pow(res, 1/base);
     return D3;
      }

     public static void Law(double v, double k) {
       if (v==k) {
     System.out.println("Pythagora works.");
      } else {
     System.out.println("Pythagora doesnt work");
    }
   }
 }

主要问题是我不确定如何回答练习所要求的问题,而且上面列出的程序不能正常工作。

3 个答案:

答案 0 :(得分:3)

  

你应该假设有一个名为raiseToPow ...

的方法

这意味着即使您没有这种方法,也可以使用这种方法编写代码。您的代码将被手动审核,或者教师可以提供方法并运行您的代码。

如果您想测试代码,可以随时自行实施。您应该在转入代码之前删除该方法。

但这里的意图是这是一个写在纸上的练习。

现在,如何实施int raiseToPow(int a, int b)

想想它意味着什么。 3 4 表示3 * 3 * 3 * 3.

因此,实施该方法将a乘以b次。

我会把它作为另一种练习留给你。

答案 1 :(得分:1)

你可以这样打破它:

public boolean checkFermat(int a, int b, int c, int n) {
    if(n != 2 &&
      (checkFermatCondition(a,b,c,n) || 
       checkFermatCondition(a,c,b,n) || 
       checkFermatCondition(b,c,a,n))) {
         System.out.println("Holy smokes, Fermat was wrong!");
    } else {
         System.out.println("No, that doesn’t work.");
    }
}

在这种方法中,您只是通过使用不同的参数调用此方法来尝试通过所有组合来减少检查条件

private boolean checkFermatCondition(int a, int b, int c, int n) {
    return raiseToPow(a,n)+raiseToPow(b,n) == raiseToPow(c,n);
}

答案 2 :(得分:0)

您的函数raiseToPow()的功能可以使用Math.pow实现:

import java.util.Scanner;

class Main {
  public static void main(String[] args) { 
    Scanner s = new Scanner(System.in); 
    System.out.println( "Fermat's Last Theorem: a^n+b^n != c^n (n!=2)"); 
    int a, b, c, n; 
    System.out.print("Enter value for a:"); 
    a = s.nextInt(); 
    System.out.print("Enter value for b:"); 
    b = s.nextInt(); 
    System.out.print("Enter value for c:"); 
    c = s.nextInt();
    while(true){
      System.out.print("Enter value for n:"); 
      n = s.nextInt();
      if(n!=2)
        break;
      System.out.println("n cannot be 2");
    }
    checkFremat(a,b,c,n);
  }

  public static void checkFremat(int a, int b, int c, int n){
    if ((int)Math.pow(a, n)+(int)Math.pow(b, n)!=(int)Math.pow(c, n)) 
      System.out.println("Fermat was correct!"); 
    else 
      System.out.println("Holy smokes, Fermat was wrong!"); 
  }
}

试试here!

相关问题