Java Factorial factorial(int n)

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

Description

factorial

License

Apache License

Parameter

Parameter Description
n Argument.

Exception

Parameter Description
IllegalArgumentException if the result is too large to be representedby a long (i.e. if n > 20).

Return

n!

Declaration

public static long factorial(int n) 

Method Source Code

//package com.java2s;
/*/*from ww w.  ja v a  2 s.  c o  m*/
 * 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.
 */

public class Main {
    /** All long-representable factorials. */
    private 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 };

    /**
     * @param n Argument.
     * @return {@code n!}
     * @throws IllegalArgumentException if the result is too large to be represented
     * by a {@code long} (i.e. if {@code n > 20}).
     */
    public static long factorial(int n) {
        return FACTORIALS[n];
    }
}

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 number)