简体   繁体   中英

How to find factorial without using Recursion or loop in java?

I need to find the factorial in java without using loop or recursion ? So if there is any way then please help . Thanks

Use Stirling approximation for Gamma function http://en.wikipedia.org/wiki/Stirling%27s_approximation

在此输入图像描述

But it will be not precise.

There is another post on here which you might like to have a look at:

Is there a method that calculates a factorial in Java?

Also - this link has lots of different implementations for factorial functions - you might find what you are looking for on here. At the very least, you will learn tons about factorials..

http://www.luschny.de/math/factorial/FastFactorialFunctions.htm

Slightly impractical but no explicit loop anywhere.

import javax.swing.Timer;
import java.awt.event.*;
import java.util.concurrent.ArrayBlockingQueue;

public class Fac {
    public static int fac(final int _n) {
        final ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1);
        final Timer timer = new Timer(0, null);
        timer.addActionListener(new ActionListener() {
            int result = 1;
            int n = _n;
            public void actionPerformed(ActionEvent e) {
                result *= n;
                n--;
                if(n == 0) {
                    try {
                        queue.put(result);
                    } catch(Exception ex) {
                    }
                    timer.stop();
                }
            }
        });
        timer.start();
        int result = 0;
        try {
            result = queue.take();
        } catch(Exception ex) {
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(fac(10));
    }
}

简单的单线解决方案,虽然在内部它正在进行循环,因为没有它就不可能,但你不需要自己做:

Long factorialNumber = LongStream.rangeClosed(2, N).reduce(1, Math::multiplyExact);

You precompute the values.

More seriously, it's not really doable, since recursion and loops are inevitable if you might need to do arbitrarily much computation.

We can do a functional factorial in Java 8 :

package com.promindis.jdk8;

import java.math.BigInteger;
import static java.math.BigInteger.*;

public class Factorial implements TCO {

  private TailCall<BigInteger> factorialTCO(
    final BigInteger fact, final BigInteger remaining) {
    if (remaining.equals(ONE))
      return done(fact);
    else
      return call(() ->
        factorialTCO(fact.multiply(remaining), dec(remaining)));
  }

  private BigInteger dec(final BigInteger remaining) {
    return remaining.subtract(ONE);
  }

  private BigInteger apply(final String from) {
    return factorialTCO(ONE, new BigInteger(from)).invoke();
  }

  public static void main(final String[] args) {
    System.out.println(new Factorial().apply("5"));
    System.out.println(new Factorial().apply("100"));

  }
}

source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM