Java 使用BigInteger类的递归查找阶乘 请考虑下面的程序段!我曾尝试使用基本递归函数来确定数字的阶乘,但现在使用BigInteger类 public static BigInteger fact(int a) { BigInteger factorial = BigInteger.ONE; BigInteger factz = BigInteger.ONE; if(a == 1) { return factorial; } else { return factz.multiply(fact(a-1)); } }
因此,当我尝试在程序中实现它时,它会将输出返回为1。是因为BigInteger对象是不可变的吗?还是我遗漏了什么?代码中有一个错误,您应该输入Java 使用BigInteger类的递归查找阶乘 请考虑下面的程序段!我曾尝试使用基本递归函数来确定数字的阶乘,但现在使用BigInteger类 public static BigInteger fact(int a) { BigInteger factorial = BigInteger.ONE; BigInteger factz = BigInteger.ONE; if(a == 1) { return factorial; } else { return factz.multiply(fact(a-1)); } },java,recursion,biginteger,factorial,Java,Recursion,Biginteger,Factorial,因此,当我尝试在程序中实现它时,它会将输出返回为1。是因为BigInteger对象是不可变的吗?还是我遗漏了什么?代码中有一个错误,您应该输入 BigInteger factz = BigInteger.valueOf(a); 而不是biginger factz=biginger.ONE看看这个: 修改内容如下: -包括0=1 -因为函数事实返回BigInteger,所以它的参数也必须是BigInteger 我没有得到局部变量的相关性,您需要使用biginger.valueOf(a) 您的
BigInteger factz = BigInteger.valueOf(a);
而不是biginger factz=biginger.ONE代码>看看这个:
修改内容如下:
-包括0=1
-因为函数事实返回BigInteger,所以它的参数也必须是BigInteger 我没有得到局部变量的相关性,您需要使用biginger.valueOf(a)
您的方法可以用一行表示:
public static BigInteger fact(int a) {
return a == 1 ? BigInteger.ONE : BigInteger.valueOf(a).multiply(fact(a - 1));
}
求阶乘,有无任何数的递归
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter no to find factorial :");
BigInteger inputNo1 = input.nextBigInteger();
System.out.println("With recursion " + inputNo1 + "! Factorial = " + (factorial(inputNo1.intValue())));
System.out.println("Without recursion " + inputNo1 + "! Factorial = " + (findFactorial(inputNo1)));
}
private static String findFactorial(BigInteger inputNo1) {
int counter;
BigInteger increment = new BigInteger("1");
BigInteger fact = new BigInteger("1");
for (counter = 1; counter <= inputNo1.longValueExact(); counter++) {
fact = fact.multiply(increment);
increment = increment.add(BigInteger.ONE);
}
return String.valueOf(fact);
}
public static BigInteger factorial(int number) {
if (number <= 1)
return BigInteger.ONE;
else
return factorial(number - 1).multiply(BigInteger.valueOf(number));
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入no以查找阶乘:”);
BigInteger inputNo1=input.nextBiginGeter();
System.out.println(“带递归”+inputNo1+“!Factorial=“+(Factorial(inputNo1.intValue())));
System.out.println(“无递归”+inputNo1+“!Factorial=“+(findFactorial(inputNo1)));
}
私有静态字符串FindFactory(BigInteger inputNo1){
整数计数器;
BigInteger增量=新的BigInteger(“1”);
BigInteger事实=新的BigInteger(“1”);
对于(counter=1;counter计算阶乘的伪代码递归如下:
function factorial(n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
使用biginger
实现它将是:
public static BigInteger factorial(BigInteger n) {
if (n.equals(BigInteger.ZERO))
return BigInteger.ONE;
else
return n.multiply(factorial(n.subtract(BigInteger.ONE)));
}
public static void main(String[] args) {
System.out.println(factorial(new BigInteger("100")));
}
输出将是:
933262154439441526816992388562667004907159682643816214685992963895217599322991608944146397615651828625369792082723723758251185210916864000000000000000000000000
注意:如果n
很大,递归会占用太多内存。在这种情况下,最好使用一些迭代算法来计算阶乘。我的解决方案是使用BigInteger类的递归来查找阶乘
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.util.*;
class Main {
public static String factorial(int n,String s){
if(n>0){
BigInteger fact = new BigInteger(s);
fact = fact.multiply(new BigInteger(n + ""));
return factorial(n-1,fact.toString());
}
else{
return s.toString();
}
}
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int n = Integer.parseInt(line);
if(n==0)
System.out.println("Factorial is 0");
else{
String s = factorial(n,"1");
System.out.println("Factorial is " + s);
}
}
}
上述代码的输出屏幕截图:
哦,好吧……我真是太蠢了!谢谢你!还有一件事,其余的都没问题吧?@SrikantaRGunnerSomayaji:0!=1,1!=1,2!=2,3!=6,4!=24等等。所以让我们测试一下:0!根本不起作用,1!2!3!…起作用。将条件更改为“if(a==0)”如果a
是int
并且fact()
使用int
作为参数,那么调用fact(a-1)
有什么问题呢?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.util.*;
class Main {
public static String factorial(int n,String s){
if(n>0){
BigInteger fact = new BigInteger(s);
fact = fact.multiply(new BigInteger(n + ""));
return factorial(n-1,fact.toString());
}
else{
return s.toString();
}
}
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int n = Integer.parseInt(line);
if(n==0)
System.out.println("Factorial is 0");
else{
String s = factorial(n,"1");
System.out.println("Factorial is " + s);
}
}
}