Java Factorial factorial(int n)

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

Description

factorial

License

Apache License

Declaration

public static double factorial(int n) 

Method Source Code

//package com.java2s;
/* //from  w w w.  j  av a 2  s  . co  m
 * Copyright 2016 Lutz Fischer <l.fischer@ed.ac.uk>.
 *
 * Licensed 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.
 */

public class Main {
    private static final double[] factorials = { 1.0 /* 0 */, 1.0 /* 1 */, 2.0 /* 2 */, 6.0 /* 3 */, 24.0 /* 4 */,
            120.0 /* 5 */, 720.0 /* 6 */, 5040.0 /* 7 */, 40320.0 /* 8 */, 362880.0 /* 9 */, 3628800.0 /* 10 */,
            39916800.0 /* 11 */, 479001600.0 /* 12 */, 6227020800.0 /* 13 */, 87178291200.0 /* 14 */,
            1307674368000.0 /* 15 */, 20922789888000.0 /* 16 */, 355687428096000.0 /* 17 */,
            6402373705728000.0 /* 18 */, 121645100408832000.0 /* 19 */, 2432902008176640000.0 /* 20 */,
            51090942171709440000.0 /* 21 */, 1124000727777607680000.0 /* 22 */, 25852016738884976640000.0 /* 23 */,
            620448401733239439360000.0 /* 24 */, 15511210043330985984000000.0 /* 25 */,
            403291461126605635584000000.0 /* 26 */, 10888869450418352160768000000.0 /* 27 */,
            304888344611713860501504000000.0 /* 28 */, 8841761993739701954543616000000.0 /* 29 */,
            265252859812191058636308480000000.0 /* 30 */, 8222838654177922817725562880000000.0 /* 31 */,
            263130836933693530167218012160000000.0 /* 32 */, 8683317618811886495518194401280000000.0 /* 33 */,
            295232799039604140847618609643520000000.0 /* 34 */,
            10333147966386144929666651337523200000000.0 /* 35 */,
            371993326789901217467999448150835200000000.0 /* 36 */,
            13763753091226345046315979581580902400000000.0 /* 37 */,
            523022617466601111760007224100074291200000000.0 /* 38 */,
            20397882081197443358640281739902897356800000000.0 /* 39 */,
            815915283247897734345611269596115894272000000000.0 /* 40 */ };
    private static final int m_lastFactorial_id = factorials.length - 1;

    public static double factorial(int n) {
        // For fast factorial calculation we assume
        // most factorial calculations we carry out are in the range 0 <= n <= 20
        // so we just do a very fast lookup in memory.
        // If the case arises where n > 20 we do the calculation
        if (n < 0) {
            throw new IllegalArgumentException("factorial can only handle n >= 0");
        }

        if (n > m_lastFactorial_id) {
            double result = factorials[m_lastFactorial_id]; // start with the last precalculated one
            for (int i = factorials.length; i <= n; i++) {
                result = result * i; // and calculate the factorial
            }
            return result;
        } else {
            // do a simple lookup
            return factorials[n]; // take the shortcut :)
        }
    }
}

Related

  1. factorial(int n)
  2. factorial(int n)
  3. factorial(int n)
  4. factorial(int n)
  5. factorial(int n)
  6. factorial(int n)
  7. factorial(int n)
  8. factorial(int n)
  9. factorial(int n)