Skip to main content

Java - Calculate Factorial



This code snippet calculates the factorial of a number up to 20. Because of the large numbers it can't go higher then 20, 21 factorial is: 51.090.942.171.709.440.000. There are two methods in this code, one for calculating it with recursion and one non-recursive.


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Factorial {
    public static void main(String[] args) {
        System.out.println(factorialRecursion(20));
        System.out.println(factorial(20));
    }
    public static long factorialRecursion(long l) {
        if (l < 2)
            return 1;
        else
            return l * factorialRecursion(l - 1);
    }
    public static long factorial(int fact) {
        long result = 1;
         
        for (int i = 1; i <= fact; ++i) {
            result *= i;
        }
        return result;
    }
}


Output:
2432902008176640000
2432902008176640000

Comments

Popular posts from this blog

Java - Varargs

Welcome to Java and varargs (short for variable arguments). In a situation where it is needed to pass in an variable amount of arguments to a method, varargs is a good answer. Some portions of this article are the same as this one: C# params. This is because they are both written by me and cover very similair topics. The code in both examples has the same idea. This is done to create a consistency for both articles. With that in mind lets continue. Before we get to the code there are a couple things to keep in mind. The varargs parameter needs to be the last in line; Only one vararg parameter is allowed.  The basic syntax is as follows: ? 1 public void methodName(parameterType... parameterName){} Notice the three dots behind parameterType. This is to tell the compiler that you want to use variable arguments. So what does the compiler in the background? It gathers all of the values and puts them into an array. The type of params can be anything...

How To Install Age Of Empires 2: Forgotten Empires

There is a new unofficial expansion pack for Age of Empires 2 called Forgotten Empires. To install Forgotten Empires you need the original Age of Empires II "Age of Kings" and the official expansion pack "The Conquerors". When Age of Empires 2 came out I could play this game non stop for hours and with this new expansion pack it looks like that's still possible even though I'm not really much of a gamer ;). Features 5  brand new civilizations 30  new technologies 9  new units 1000  population limit 11  new maps 1  new building 2  new architecture sets 20  new scenario editor objects ?  new campaigns Full  unlimited  resolutions (Widescreen included) Install Make sure to have Age of Empires II: The Conquerors installed Download the installer exe | zip Unzip the file into your Age of Empires II Installation folder (typically C:\Program Files\Microsoft Games\Age of Empires II\) Select your ...

Minecraft - Feed the Beast launcher - Part 1

Have you tried to implement a mod in Minecraft? All the research on how to do it, if mod version x.xx is compatible for your current Minecraft version and a lot more. Thanks to the FTB launcher, which is short for Feed the Beast Launcher, that is ancient history. FTB launcher is an program designed to play modpacks without any configuring. With just a couple of clicks you can start an available modpack and the launcher will download en configure everything for you. It has the ability to download maps, texturepacks, different mod packs and more. The official site can be found here: http://www.feed-the-beast.com Because there is a lot to talk about this article will be split into two parts. The first will cover the following topics of the launcher: News; Options; Maps; Texturepacks. The second part will contain the most important feature of the launcher, which is the Modpack section. The modpack section will be divided into the following topics: Modpacks ...