factorial.java

来自「a molecular graph kernel based on iterat」· Java 代码 · 共 110 行

JAVA
110
字号
/** * ISOAK - Iterative similarity optimal assignment kernel. *  * Written by Matthias Rupp 2006-2007. * Copyright (c) 2006-2007, Matthias Rupp, Ewgenij Proschak, Gisbert Schneider. *  * All rights reserved. *  * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: *  *  * The above copyright notice, this permission notice and the following disclaimers *    shall be included in all copies or substantial portions of this software. *  * The names of the authors may not be used to endorse or promote products *    derived from this software without specific prior written permission. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. *  * Please cite *  * Matthias Rupp, Ewgenij Proschak, Gisbert Schneider: * A Kernel Approach to Molecular Similarity Based on Iterative Graph Similarity, * Journal of Chemical Information and Molecular Modeling, 47(6): 2280-2286, 2007,  * DOI http://dx.doi.org/10.1021/ci700274r. * * if you use this software. */package info.mrupp.isoak1;/**  * Precomputed factorial function. *  * Returns the factorial n*(n-1)*...*1 of the input argument n. *  * Implemented via tables for maximum speed. Runs in constant time. */public class Factorial {	private static final int[] intTable = {         1,         // 0!		1,         // 1!		2,         // 2!		6,         // 3!        24,        // 4!        120,       // 5!        720,       // 6!        5040,      // 7!        40320,     // 8!        362880,    // 9!        3628800,   // 10!        39916800,  // 11!        479001600  // 12!	};		private static final long[] longTable = {        1L,                   // 0!		1L,                   // 1!		2L,                   // 2!		6L,                   // 3!        24L,                  // 4!        120L,                 // 5!        720L,                 // 6!        5040L,                // 7!        40320L,               // 8!        362880L,              // 9!        3628800L,             // 10!        39916800L,            // 11!        479001600L,           // 12!        6227020800L,          // 13!        87178291200L,         // 14!        1307674368000L,       // 15!        20922789888000L,      // 16!        355687428096000L,     // 17!        6402373705728000L,    // 18!        121645100408832000L,  // 19!        2432902008176640000L  // 20!	};     // Public interface.        /** Provides precomputed results for the factorial function. */    public Factorial() {}    /** Returns the factorial for arguments 0,1,...,12.     *    * Argument range is limited due to 13! being larger than the maximum integer.    */	public static int factorial(int n) { 		try { return intTable[n]; }		catch(IndexOutOfBoundsException e) { throw new ArithmeticException(e.getMessage()); }	}    /** Returns the factorial for arguments 0,1,...,20.     *      * Argument range is limited due to 21! being larger than the maximum long integer.     */	public static long factorial(long n) { 		try { return longTable[(int) (n)]; }		catch(IndexOutOfBoundsException e) { throw new ArithmeticException(e.getMessage()); }	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?