java 费马大定理(Fermat's Last Theorem)

inkz8wg9  于 2023-03-28  发布在  Java
关注(0)|答案(3)|浏览(103)

我正在努力学习Java;以下是我正在努力的练习:
费马大定理说,除了n = 2的情况之外,不存在整数a、B和c使得a^n + b^n = c^n。
写一个名为checkFermat的方法,它接受四个整数作为参数-- a、B、c和n--并检查费马定理是否成立。如果n大于2,并且结果证明a^n + b^n = c^n是真的,程序应该输出“Holy smokes,Fermat was wrong!”否则程序应该输出“No,that doesn 't work.”
你应该假设有一个名为raiseToPow的方法,它接受两个整数作为参数,并将第一个参数提升到第二个参数的幂。例如:int x = raiseToPow(2, 3);会给x赋值8,因为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");
    }
   }
 }

主要的问题是,我不完全确定如何回答练习中提出的问题,上面列出的程序也没有按照它应该的那样工作。

5t7ly7z5

5t7ly7z51#

你应该假设有一个名为raiseToPow的方法...
这意味着你使用这样的方法编写代码,即使你没有方法。你的代码将被手动检查,或者老师可以提供方法并运行你的代码。
如果你想测试你的代码,你可以自己实现它。你应该在提交代码之前删除方法。
但这里的意图是,这是一个写在纸上的练习。
那么,如何实现int raiseToPow(int a, int b)呢?
想想它的意思。34的意思是3 * 3 * 3 * 3。
因此,实现方法乘以a乘以自身b次。
我会把它作为另一个练习留给你。

2wnc66cl

2wnc66cl2#

您可以像这样拆分它:

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

zzoitvuj3#

你的函数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!

相关问题