0

I can do it by using a for loop but it's too long. I have to do about 10 for-loops.

 System.out.println("factorial of 10");
 int fact = 1;
 for (int x=1;x<=10;x++) {
     fact=fact*x;
     System.out.println(fact);
 }

 System.out.println(" factorial of 20 ");
 double fact1 = 1;
 for (double y=1;y<=20;y++){
     fact1=fact1*y;
     System.out.println(fact1);
 }
Mat
  • 202,337
  • 40
  • 393
  • 406
Abdalla
  • 3
  • 3

3 Answers3

2

You can use recursive methods for computing factorials.Also Adjust the type according to you in the solution.Like BigInteger or Long

public class Factorial {
    public static void main (String[] args) {
        int theNum, theFact;
        System.out.println("This program computes the factorial " +
                "of a number.");
        System.out.print("Enter a number: ");
        theNum=Console.in.readInt();
        theFact = fact(theNum);
        System.out.println(theNum + "! = " + theFact + ".");
    }

    static int fact(int n) {
        if (n <= 1) {
            return 1;
        }
        else {
            return n * fact(n-1);
        }
    }
}
Deepak
  • 2,287
  • 1
  • 23
  • 30
  • the return value of `fact` should be a `BigInteger`, the OP wants to be able to calculate 100!, which does not fit in a `int` – amit Dec 25 '13 at 13:24
  • @amit thanks I will make a note of that in the answer – Deepak Dec 25 '13 at 13:27
1

You can put every calculated factorial to an array and calculate new factorial value by retrieving last element of array. But, just so you know, you can calculate up to 12! with type int because with 13!, numbers become greater than int's MAX_VALUE(2^31-1). You can use BigInteger objects as a solution.

Or if you just have to calculate those specific numbers' factorial values, you can call a recursive function which works like solution above.

1

Use Stirling approximation for Gamma function

enter image description here

You can use this equation to do the trick. It's not much of a computer language if it has no loops.


Edit: Or you can use the following code with no explicit loop anywhere (borrowed from here).

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));
    }
}
Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174