Here you can find the source of factorial(long n)
Parameter | Description |
---|---|
n | long integer |
public static long factorial(long n)
//package com.java2s; /**/*from w w w . j a v a 2 s . co m*/ * Copyright notice * * This file is part of the Processing library `gwoptics' * http://www.gwoptics.org/processing/gwoptics_p5lib/ * * Copyright (C) 2009 onwards Daniel Brown and Andreas Freise * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License version 2.1 as published * by the Free Software Foundation. * * 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., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class Main { /** * This function computes the factorial of an long integer * * @param n long integer * @return factorial n! */ public static long factorial(long n) { long fac = 1; if (n < 0) { throw new RuntimeException("Underflow error in factorial"); } else if (n > 20) { throw new RuntimeException("Overflow error in factorial"); } else if (n == 0) { return 1; } else { for (int i = 1; i <= n; i++) { fac = fac * i; } } return fac; } }