Java Factorial Using BigInteger

Solving Factorial of number by using BigInteger in Java


  import java.util.*;
  import java.math.BigInteger;
  class cf
  {
      static HashMap<BigInteger,BigInteger> list = new HashMap<>();
      static BigInteger extraLongFactorials(int n) {
          BigInteger N = new BigInteger(String.valueOf(n));
          if(list.containsKey(N)) return list.get(N);
          if(n==1 || n==2 || n==0) 
              return N;
          else {
              BigInteger ans = N.multiply(extraLongFactorials(n-1));
              list.put(N,ans);
              return list.get(N);
          }

      }

    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
      try
      {
              int n = input.nextInt();
              System.out.println(extraLongFactorials(n));
      }
      catch(Exception e){
        return;
      }
    }
  }

  

Output :

  100
  93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Popular Posts

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Family Tree Project in Java

Creating basic tic tac toe android app using java