The Factorial in Java

I’ve just been writing a blog about the factorial function, and I want to share with you a few observations I made.

First off, it is good to be reminded that there is a looping solution to the problem. It’s not obvious that there would be an elegant looping solution to the problem. I didn’t know what to do at first, and I spent a lot of time looking for a subroutine declaration I could use. But then I realized that the language had built-in support for defining your own functions. So all I needed was some code to define factorial as a function, and then reference that function from elsewhere in my program.

This is one reason why functional programming is so powerful: it gives you access to things you’d never think of looking for yourself, because you don’t realize they’re there.

I am not going to explain the factorial function here, but I will show you how to make up a simple loop that is needed to calculate it.

The factorial of a number X is defined by the formula:

Fact(n) = 1 * Fact(n-1) * Fact(n-2) * … * Fact(1) The loop in java is: for (int i = 2; i <= n; i++) for (int j = 2; j <= i; j++) System.out.println((i*j) + "!"); Factorial is one of those functions that is hard to explain without a reference. The factorial of n! is n times n! but it's also the product of some other numbers: 1 * 2 * 3 * ... * n. In terms of loops, you have to loop through the numbers from 1 to n - 1, and apply the factorial function to each number in turn. I wrote this blog post because I was learning Java and needed a way to understand how the factorial function works under Java's default looping semantics. The first part explains how it might be implemented in C or Python; then I showed how it could be done in Java with Integer arithmetic and a tail recursive loop. The factorial in Java looks like this: public static int factorial(int n) { return 1; } That's it? That's all there is to the factorial function in Java? Feel free to stop reading now. It's hard to be excited about a thing that can be written so simply and does such a simple thing. It used to be much more complicated, though. In fact, until fairly recently, it was almost as complicated as the factorial function in C++: for(int i=0; i < n; ++i) { if (i == 0) return 1; } That was at least twice as complicated as the Java version. And it didn't even do what we want: if i == 0, return -1 instead of returning 1. Factorial is a function that calculates the factorial of a number, which is the product of all the numbers from 1 to the number you give it. Here is an example: factorial(1) = 1 factorial(2) = 2 * 1 = 2 factorial(3) = 3 * 2 = 6 This function has two main parts. The first is that when you give it a number, it returns a number, so on. But then there is a loop where the code calculates one more number and goes into that code again. The inner loop runs exactly once; the outer loop runs at least once because there are at least two numbers. In Java: import static java.lang.Math.*; public class FactorialDemo { public static void main(String[] args) { //the factorial of any non-zero number int factorial = Math.factorial(5); System.out.println("5! = " + (factorial * 5)); } } The factorial of a number is the product of all the numbers from 1 to that number. For example, the factorial of 5 is 25 because: factorial(1) = 1 factorial(2) = 2 * factorial(1) = 2 * 1 = 2 factorial(3) = 3 * factorial(2) = 3 * 2 = 6 factorial(4) = 4 * 3 * 4 * 5 * 6 * 7 * 8 = 24 To compute the factorial of a number, we can loop through all the numbers from 1 to the number we want and add up their product. If we'd like to compute the factorial of 6, for example, we might do this: for (int i=1; i<=6; i++) { The result is just 24. The factorial of a number is the product of all the positive integers less than or equal to that number, starting with 1. The Java Factorial class makes this easy by providing an instance method named

Leave a Reply