威尔逊定理的证明 程序代码 c/c++/Java(最好是Java)代码要全 非诚勿扰
发布网友
发布时间:2022-05-02 12:05
我来回答
共1个回答
热心网友
时间:2023-10-05 10:44
import java.math.BigInteger;
import java.util.Scanner;
public class WilsonTheorem {
public static void main(String[] args) {
System.out.println("请输入一个质数:");
Scanner in = new Scanner(System.in);
int p;
while (true) {
try {
p = in.nextInt();
if (!judge(p))
throw new Exception();
BigInteger factorial = new BigInteger("1");
for(int i = 2; i < p; i++) {
factorial = factorial.multiply(new BigInteger(i + ""));
}
factorial = factorial.add(new BigInteger("1"));
System.out.println("(p-1)! + 1 = " + factorial);
BigInteger result = factorial.divide(new BigInteger(p + ""));
System.out.println("(p-1)! + 1除以p = " + result);
} catch (Exception e) {
System.out.println("输入错误,请重新输入!");
continue;
}
}
}
/**
* 判断一个是否是质数的方法
*
* @param num
* @return
*/
public static boolean judge(int num) {
int i;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0)
break;
}
return i > num / 2 ? true : false;
}
}