Question: What is wrong with the following method for calculating factorials? /** * Calculates the factorial of a non-negative integer, that is, the product of *
What is wrong with the following method for calculating factorials?
/** * Calculates the factorial of a non-negative integer, that is, the product of * all integers between 1 and the given integer, inclusive. * The worstTime(n) is O(n), where n is the given integer.
*
* @param n the non-negative integer whose factorial is calculated. *
* @return the factorial of n. *
* @throws IllegalArgumentException if n is less than 0. *
*/ public static long factorial (int n) {
if (n < 0) throw new IllegalArgumentException( );
if (n <= 1) return 1;
return fact (n+1) / (n+1); } // fact
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
