Factorial of numbers greater than or equal to xiii cannot hold upwardly flora using primitive int data type equally shown inwards our before factorial solution due to overflow. These factorials are likewise large to gibe inwards an int variable, whose maximum value is only 2147483647 (2^31 -1). Even if nosotros role the long information type, factorials greater than or equal to 21 volition generate an overflow. To notice the factorial of anything higher upwardly 21, y'all involve to role the BigInteger degree from java.math package. As the refer suggests, BigInteger class is designed to concur actually large integer value, something which is fifty-fifty bigger than the maximum value of long primitive e.g. 2^63 -1 or 9223372036854775807L. You also involve to modify the way nosotros calculate factorial for a smaller number. You tin non role recursion to calculate factorial of a larger unwrap instead nosotros involve to role for loop for that.
Also worth noting that, similar to java.lang.String and other wrapper classes BigInteger is also Immutable inwards Java, which way it's of import to shop the trial dorsum into the same variable, otherwise, the trial of the calculation volition hold upwardly lost. BigInteger stores numbers equally 2's complement unwrap similar int primitive too back upwardly functioning supported yesteryear int variables too all relevant methods from java.lang.Math class.
Additionally, it also provides back upwardly for modular arithmetic, flake manipulation, primality testing, prime number generation, GCD calculation too other miscellaneous operations.
You tin run across that how large factorial of 45 is, clearly it's non possible to role long information type to shop such huge integral values. You involve to role BigInteger degree to shop such large values.
BTW, If y'all are looking for or thus programming practise to create coding interview or to developer your programming logic too thus y'all should depository fiscal establishment check problems from Cracking the Coding Interview: 189 Programming Questions too Solutions, 1 of the best majority for preparing coding interviews.
1. The BigInteger degree is used to stand upwardly for arbitrarily large numbers. Overflow doesn't tumble out equally is the illustration amongst int too long primitive.
2. The BigInteger degree is immutable which way that the object on which the multiply share was invoked doesn't modify the integer it is holding. The multiplication is performed too a novel BigInteger is returned which needs to hold upwardly stored inwards the variable fact.
3. BigInteger provides operations similar to int primitive type inwards Java, additionally, it provides back upwardly for the prime number generation, flake manipulation, GCD calculations etc.
4. You tin create BigInteger object yesteryear giving unwrap equally String or byte array using constructor, or y'all tin convert a long value to BigInteger using valueOf() method equally shown below :
Remember BigInteger can assistance y'all to bargain amongst actually large numbers inwards Java.
That's all most how to calculate factorial of a large unwrap inwards Java. Clearly later or thus betoken long is non plenty to trial of factorial too y'all involve something bigger than long but non double, BigInteger is the degree to stand upwardly for large integral values. In theory, BigInteger has no confine too it tin stand upwardly for whatever integral value till infinity.
Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures too Algorithms: Deep Dive Using Java
solution)How to impress all permutations of a String inwards Java? (solution) How to contrary an array inwards house inwards Java? (answer) How to depository fiscal establishment check if given String is Palindrome inwards Java? (solution) How to write FizzBuzz inwards Java 8? (answer) How to contrary Integer inwards Java? (solution) How to notice starting fourth dimension non repeated graphic symbol from String? (solution) Questions from Coding Puzzles: Thinking inwards code By codingtmd? (see here) Questions from Programming Interviews Exposed: Secrets to Landing Your Next Job? (see here)
Also worth noting that, similar to java.lang.String and other wrapper classes BigInteger is also Immutable inwards Java, which way it's of import to shop the trial dorsum into the same variable, otherwise, the trial of the calculation volition hold upwardly lost. BigInteger stores numbers equally 2's complement unwrap similar int primitive too back upwardly functioning supported yesteryear int variables too all relevant methods from java.lang.Math class.
Additionally, it also provides back upwardly for modular arithmetic, flake manipulation, primality testing, prime number generation, GCD calculation too other miscellaneous operations.
Java Program to Calculate Factorial of Large Number
Here is our sample Java computer program to calculate factorial for large numbers, well, given unwrap is non just large but the factorial value is definitely large. For example, the factorial of 45 is 119622220865480194561963161495657715064383733760000000000, which is clearly out of saltation for fifty-fifty a long information type. Since theoretically BigInteger has no confine it tin concur these values equally shown inwards the next example. You volition also notice that instead of recursion, nosotros convey used iteration to calculate factorial inwards Java.import java.math.BigInteger; /** * Write a Java computer program to calculate factorial of large numbers using * BigInteger. * * @author WINDOWS 8 * */ public class LargeFactorialDemo { public static void main(String args[]) { System.out.printf("Factorial of 32 is %s %n", factorial(32)); System.out.printf("Factorial of 0 is %s %n", factorial(0)); System.out.printf("Factorial of 1 is %s %n", factorial(1)); System.out.printf("Factorial of v is %s %n", factorial(5)); System.out.printf("Factorial of 41 is %s %n", factorial(41)); System.out.printf("Factorial of 45 is %s %n", factorial(45)); } /* * Java method to calculate factorial of a large unwrap * @return BigInteger factorial of given unwrap */ public static BigInteger factorial(int number) { BigInteger factorial = BigInteger.ONE; for (int i = number; i > 0; i--) { factorial = factorial.multiply(BigInteger.valueOf(i)); } return factorial; } } Output Factorial of 32 is 263130836933693530167218012160000000 Factorial of 0 is 1 Factorial of 1 is 1 Factorial of 5 is 120 Factorial of 41 is 33452526613163807108170062053440751665152000000000 Factorial of 45 is 119622220865480194561963161495657715064383733760000000000
You tin run across that how large factorial of 45 is, clearly it's non possible to role long information type to shop such huge integral values. You involve to role BigInteger degree to shop such large values.
BTW, If y'all are looking for or thus programming practise to create coding interview or to developer your programming logic too thus y'all should depository fiscal establishment check problems from Cracking the Coding Interview: 189 Programming Questions too Solutions, 1 of the best majority for preparing coding interviews.
Important things most BigInteger degree inwards Java
BigInteger degree inwards Java is designed to bargain amongst actually large numbers inwards Java, but to produce that it's real of import that y'all brand yourself familiar amongst the class. Here are or thus cardinal points most java.math.BigInteger degree :1. The BigInteger degree is used to stand upwardly for arbitrarily large numbers. Overflow doesn't tumble out equally is the illustration amongst int too long primitive.
2. The BigInteger degree is immutable which way that the object on which the multiply share was invoked doesn't modify the integer it is holding. The multiplication is performed too a novel BigInteger is returned which needs to hold upwardly stored inwards the variable fact.
3. BigInteger provides operations similar to int primitive type inwards Java, additionally, it provides back upwardly for the prime number generation, flake manipulation, GCD calculations etc.
4. You tin create BigInteger object yesteryear giving unwrap equally String or byte array using constructor, or y'all tin convert a long value to BigInteger using valueOf() method equally shown below :
BigInteger bigIntegerFromLong = BigInteger.valueOf(292909333L); BigInteger bigIntegerFromString = new BigInteger("338948938948");
Remember BigInteger can assistance y'all to bargain amongst actually large numbers inwards Java.
That's all most how to calculate factorial of a large unwrap inwards Java. Clearly later or thus betoken long is non plenty to trial of factorial too y'all involve something bigger than long but non double, BigInteger is the degree to stand upwardly for large integral values. In theory, BigInteger has no confine too it tin stand upwardly for whatever integral value till infinity.
Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures too Algorithms: Deep Dive Using Java
solution)