Here you can find the source of factorial(int value)
Parameter | Description |
---|---|
value | some value between 0 and 12. 13! is larger than MAX_INTEGER |
public static int factorial(int value)
//package com.java2s; /*/*from w w w . jav a 2s.co m*/ * Copyright 2016 Roche NimbleGen Inc. * * 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 { /** * @param value * some value between 0 and 12. 13! is larger than MAX_INTEGER * @return the factorial of the given number */ public static int factorial(int value) { if (value > 12) { throw new IllegalStateException("The value[" + value + "] is greater than 12 and anything equal to or larger than 13! is too large to be returned as an int."); } else if (value < 0) { throw new IllegalStateException( "The value[" + value + "] is less than 0, so factorial cannot be computed."); } int result = 1; for (int i = 2; i <= value; i++) { result *= i; } return result; } /** * @param value * some value between 0 and 199. 200! is larger than MAX_LONG * @return the factorial of the given number */ public static long factorial(long value) { if (value > 199) { throw new IllegalStateException("The value[" + value + "] is greater than 199 and anything equal to or larger than 200! is too large to be returned as an long."); } else if (value < 0) { throw new IllegalStateException( "The value[" + value + "] is less than 0, so factorial cannot be computed."); } long result = 1; for (long i = 2; i <= value; i++) { result *= i; } return result; } }