Package edu.cnm.deepdive
Class Factorials
java.lang.Object
edu.cnm.deepdive.Factorials
Implements recursive and iterative forms of factorial computation, returning
long
and
BigInteger
results. (When n > 20, n! exceeds the range of values that
can be represented by the long
type.)
Note that for large values of n, the computation of n! is best done by methods other than those included here.
-
Method Summary
Modifier and TypeMethodDescriptionstatic BigInteger
bigIterative
(int n) Computes (recursively) and returns the factorial ofn
as aBigInteger
.static BigInteger
bigRecursive
(int n) Computes (recursively) and returns the factorial ofn
as aBigInteger
.static long
longIterative
(int n) Computes (iteratively) and returnsn!
as along
.static long
longRecursive
(int n) Computes (recursively) and returnsn!
as along
.
-
Method Details
-
longRecursive
Computes (recursively) and returnsn!
as along
. However, if(n > 20)
, the result overflows the range oflong
, and is meaningless.- Parameters:
n
- Value for which factorial is computed.- Returns:
- Factorial of
n
; that is,n!
. - Throws:
IllegalArgumentException
- If(n < 0)
or(n > 20)
.
-
longIterative
Computes (iteratively) and returnsn!
as along
. However, if(n > 20)
, the result overflows the range oflong
, and is meaningless.- Parameters:
n
- Value for which factorial is computed.- Returns:
- Factorial of
n
; that is,n!
. - Throws:
IllegalArgumentException
- If(n < 0)
or(n > 20)
.
-
bigRecursive
Computes (recursively) and returns the factorial ofn
as aBigInteger
.- Parameters:
n
- Value for which factorial is computed.- Returns:
- Factorial of
n
; that is,n!
. - Throws:
IllegalArgumentException
- If(n < 0)
.
-
bigIterative
Computes (recursively) and returns the factorial ofn
as aBigInteger
.- Parameters:
n
- Value for which factorial is computed.- Returns:
- Factorial of
n
; that is,n!
. - Throws:
IllegalArgumentException
- If(n < 0)
.
-