2

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

user1829457
  • 31
  • 1
  • 3

6 Answers6

7

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

enter image description here

But it will be not precise.

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
2

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

Community
  • 1
  • 1
Penelope The Duck
  • 656
  • 1
  • 7
  • 16
1

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));
    }
}
Tesseract
  • 8,049
  • 2
  • 20
  • 37
1

Simple one liner solution, though internally it is doing a loop, as it can't possible without it, but you don't need to do it yourselves:

Long factorialNumber = LongStream.rangeClosed(2, N).reduce(1, Math::multiplyExact);
krmanish007
  • 6,749
  • 16
  • 58
  • 100
0

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
-1

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

Graham Griffiths
  • 2,196
  • 1
  • 12
  • 15
  • although TBH both this and SpiderPig's solutions are just playing with semantics...it's recursion, but not your regular 'function call' type recursion. – Graham Griffiths Aug 23 '13 at 15:46