日期:2014-05-20  浏览次数:20763 次

求阶乘!!!!
求999的阶乘!!!!

------解决方案--------------------
Java code

protected static ArrayList<BigInteger> list = new ArrayList<BigInteger>();
    static {
        list.add(BigInteger.valueOf(1));
    }
    public static BigInteger factorial(int x) {
        for (int size = list.size(); size <= x; size++) {
            BigInteger lastfact = (BigInteger) list.get(size - 1);
            BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
            list.add(nextfact);
        }
        return (BigInteger)list.get(x);
    }

    public static void main(String[] args) {

        System.out.println(999 + "!=" + factorial(6));
    }

------解决方案--------------------
Java code
public final class Factorial {

    /**
     * Default thread count(CPU count * 4)
     */
    static final int THREAD_COUNT;
    static {
        THREAD_COUNT = Runtime.getRuntime().availableProcessors() << 2;
    }

    /**
     * Never instantiate this class.
     */
    private Factorial() {
    }

    /**
     * Calculates the factorial of n. A new thread pool will be initialized to
     * perform the calculation task, and destroyed thereafter.
     * 
     * @param n
     *            the value of n
     * @return the factorial of the given n
     * @throws Exception
     *             if exception occurs
     */
    public static final BigInteger factorial(final int n) throws Exception {
        ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);
        try {
            return factorial(n, threadPool, THREAD_COUNT);
        } finally {
            threadPool.shutdown();
        }
    }

    /**
     * Calculates the factorial of n. A specified thread pool will be used to
     * perform the calculation task, and will not be automatically destroyed.
     * 
     * @param n
     *            the value of n
     * @param threadPool
     *            the given thread pool to perform the calculation task
     * @param threadCount
     *            the number of threads to perform the calculation task
     * @return the factorial of the given n
     * @throws Exception
     *             if exception occurs
     */
    public static BigInteger factorial(final int n, ExecutorService threadPool, final int threadCount) throws Exception {

        List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(threadCount);
        for (int i = 1; i <= threadCount; i++) {
            final int threadNo = i;
            Callable<BigInteger> worker = new Callable<BigInteger>() {

                @Override
                public BigInteger call() throws Exception {
                    BigInteger result = BigInteger.ONE;
                    for (int x = threadNo; x <= n; x += threadCount) {
                        result = BigInteger.valueOf(x).multiply(result);
                    }
                    return result;
                }

            };
            tasks.add(worker);
        }

        BigInteger result = BigInteger.ONE;
        List<Future<BigInteger>> futureList = threadPool.invokeAll(tasks);
        for (Future<BigInteger> future : futureList) {
            result = future.get().multiply(result);
        }
        return result;
    }
}

------解决方案--------------------
Java code
import java.math.BigInteger;

public class Test {

    public static void main(String args[]) {
        BigInteger bi = factorial(999);
        System.out.println(bi);
    }
    
    public static BigInteger factorial(int n) {
        if(n < 2) {
            return BigInteger.ONE;
        }
        int[] oddCount = new int[Integer.numberOfTrailingZeros(Integer.highestOneBit(n))];
        int shift = init(oddCount, n);
        BigInteger result = BigInteger.ONE;
        BigInteger bg = BigInteger.ONE;
        BigInteger tmp = BigInteger.ONE;

        int max = oddCount[oddCount.length - 1];
        int offset = (oddCount[0] + 1) & 1;
        int oddStart = 1;
        while(oddStart <= max) {
            tmp = tmp.multiply(new BigInteger(String.valueOf(2 * oddStart + 1)));
            if(oddCount[offset] == oddStart) {
                offset++;
                bg = bg.multiply(tmp);
                tmp = BigInteger.ONE;
                result = result.multiply(bg);
            }
            oddStart++;
        }
        return result.shiftLeft(shift);
    }
}