简体   繁体   English

如何在java中不使用递归或循环找到阶乘?

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

I need to find the factorial in java without using loop or recursion ? 我需要在不使用循环或递归的情况下在java中找到阶乘? So if there is any way then please help . 所以,如果有任何方法,请帮助。 Thanks 谢谢

Use Stirling approximation for Gamma function http://en.wikipedia.org/wiki/Stirling%27s_approximation 使用斯特林近似Gamma函数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? 有没有一种方法可以计算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 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 : 我们可以在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 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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