Finding factorial using recursion with class BigInteger

So, consider the next program segment! I tried using the basic recursion function to determine the factorial of a number, but now I use the BigInteger class.

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

Therefore, when I try to implement this in a program, it returns the result as 1. Is it because BigInteger objects are immutable? Or am I missing something here?

+4
source share
6 answers

There is an error in the code, you must put

  BigInteger factz = BigInteger.valueOf(a); 

instead of BigInteger factz = BigInteger.ONE;

+3
source

The factorial calculation pseudo code recursively looks like this:

 function factorial(n) { if (n == 0) return 1; else return n * factorial(n - 1); } 

Implementing it using BigInteger will be:

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

The output will be:

933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000000000

Note : recursion takes up too much memory if n is large. In this case, it is better to use some iterative algorithm to calculate the factorial.

+3
source

I do not understand the relevance of local variables, and you need to use BigInteger.valueOf(a) .

Your method can be expressed in only one line:

 public static BigInteger fact(int a) { return a == 1 ? BigInteger.ONE : BigInteger.valueOf(a).multiply(fact(a - 1)); } 
+1
source

Find a factorial with recursion of any number and without it.

 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)); } 
0
source

My solution for finding factorial using recursion with class 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); } } } 

Screenshot for the above code: Display screenshot for above code

0
source

Take a look at this:

  public static BigInteger fact(BigInteger a) { if(a.intValue()==1||a.intValue()==0) { return BigInteger.ONE; } else { return a.multiply(fact(a.subtract(BigInteger.ONE))); } } 

Modifications:
- Include 0! = 1
- Since the function returns a BigInteger, its argument must be BigInteger too!

-one
source

Source: https://habr.com/ru/post/1493916/


All Articles