Java Factorial factorialLog(final int n)

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

Description

Returns the natural logarithm of n!.

License

Open Source License

Parameter

Parameter Description
n argument

Exception

Parameter Description
IllegalArgumentException if preconditions are not met.

Return

n!

Declaration

public static double factorialLog(final int n) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  . j  a  v  a  2 s  .c  o m
 * Copyright (c) 2006-2011 Karsten Schmidt
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * http://creativecommons.org/licenses/LGPL/2.1/
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

public class Main {
    /** All long-representable factorials */
    private static final long[] factorials = new long[] { 1, 1, 2, 6, 24,
            120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600,
            6227020800l, 87178291200l, 1307674368000l, 20922789888000l,
            355687428096000l, 6402373705728000l, 121645100408832000l,
            2432902008176640000l };

    /**
     * Returns the natural logarithm of n!.
     * <p>
     * <Strong>Preconditions</strong>:
     * <ul>
     * <li> <code>n >= 0</code> (otherwise
     * <code>IllegalArgumentException</code> is thrown)</li>
     * </ul></p>
     * 
     * @param n argument
     * @return <code>n!</code>
     * @throws IllegalArgumentException if preconditions are not met.
     */
    public static double factorialLog(final int n) {
        if (n < 0) {
            return 0;
        }
        if (n < 21) {
            return Math.log(factorial(n));
        }
        double logSum = 0;
        for (int i = 2; i <= n; i++) {
            logSum += Math.log(i);
        }
        return logSum;
    }

    /**
     * Returns n!. Shorthand for <code>n</code> <a
     * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the
     * product of the numbers <code>1,...,n</code>.
     * <p>
     * <Strong>Preconditions</strong>:
     * <ul>
     * <li> <code>n >= 0</code> (otherwise
     * <code>IllegalArgumentException</code> is thrown)</li>
     * <li> The result is small enough to fit into a <code>long</code>. The
     * largest value of <code>n</code> for which <code>n!</code> <
     * Long.MAX_VALUE</code> is 20. If the computed value exceeds <code>Long.MAX_VALUE</code>
     * an <code>ArithMeticException </code> is thrown.</li>
     * </ul>
     * </p>
     * 
     * @param n argument
     * @return <code>n!</code>
     * @throws ArithmeticException if the result is too large to be represented
     *         by a long integer.
     * @throws IllegalArgumentException if n < 0
     */
    public static long factorial(final int n) {
        if (n < 0) {
            return 0;
        }
        if (n > 20) {
            return 0;
        }
        return factorials[n];
    }
}

Related

  1. factorial(long num)
  2. factorialAsDouble(int n)
  3. factorialAsDoubleIncludeDivisor(int n, double divisor)
  4. factorialCheckBounds(int n)
  5. factorialDouble(final int n)
  6. factorialLog(final int n)