Android Factorial factorial(final int n)

Here you can find the source of factorial(final int n)

Description

Returns n!.

License

Apache License

Parameter

Parameter Description
n argument

Exception

Parameter Description
MathArithmeticException if n > 20: The factorial value is toolarge to fit in a long.
NotPositiveException if n < 0.

Return

n!

Declaration

public static long factorial(final int n) throws NotPositiveException,
        MathArithmeticException 

Method Source Code

/*/*from ww  w .j a  v  a  2 s  .  c  om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.math3.exception.MathArithmeticException;
import org.apache.commons.math3.exception.NotPositiveException;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.util.Localizable;
import org.apache.commons.math3.exception.util.LocalizedFormats;

public class Main{
    /** All long-representable factorials */
    static final long[] FACTORIALS = new long[] { 1l, 1l, 2l, 6l, 24l,
            120l, 720l, 5040l, 40320l, 362880l, 3628800l, 39916800l,
            479001600l, 6227020800l, 87178291200l, 1307674368000l,
            20922789888000l, 355687428096000l, 6402373705728000l,
            121645100408832000l, 2432902008176640000l };
    /**
     * Returns n!. Shorthand for {@code n} <a
     * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the
     * product of the numbers {@code 1,...,n}.
     * <p>
     * <Strong>Preconditions</strong>:
     * <ul>
     * <li> {@code n >= 0} (otherwise
     * {@code IllegalArgumentException} is thrown)</li>
     * <li> The result is small enough to fit into a {@code long}. The
     * largest value of {@code n} for which {@code n!} <
     * Long.MAX_VALUE} is 20. If the computed value exceeds {@code Long.MAX_VALUE}
     * an {@code ArithMeticException } is thrown.</li>
     * </ul>
     * </p>
     *
     * @param n argument
     * @return {@code n!}
     * @throws MathArithmeticException if the result is too large to be represented
     * by a {@code long}.
     * @throws NotPositiveException if {@code n < 0}.
     * @throws MathArithmeticException if {@code n > 20}: The factorial value is too
     * large to fit in a {@code long}.
     */
    public static long factorial(final int n) throws NotPositiveException,
            MathArithmeticException {
        if (n < 0) {
            throw new NotPositiveException(
                    LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n);
        }
        if (n > 20) {
            throw new MathArithmeticException();
        }
        return FACTORIALS[n];
    }
}

Related

  1. factorialDouble(final int n)
  2. factorialLog(final int n)