An algorithm a day : Calculate the factorial of a number

Marina Shemesh
4 min readJul 6, 2017

--

Background image created by Bedneyimages — Freepik.com

What is a factorial?

In maths, the factorial of a non-negative integer, is the product of all positive integers less than or equal to this non-negative integer.

The symbol for the factorial function is an exclamation mark after a number.

For example: 5!

So if you see something like 5! , you know that you will have to multiply the series of descending possitive integers.

For example:

5! = 5 * 4 * 3 * 2 * 1 = 120

and

4! = 4 * 3 * 2 * 1 = 24

Integers or whole numbers or natural numbers?

If you google around a bit you will notice that the definition of “what is a factorial” differs a bit from each other:

Mathsisfun.com:

The result of multiplying a sequence of descending natural numbers down to 1 (such as 4 × 3 × 2 × 1).

Decodedscience.org:

The usual definition of “n factorial” is “n! = n * (n — 1) * (n — 2) *…* 2 * 1,” where ’n’ is a positive integer.

Wikipedia.org:

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

The use of non-negative integers, positive integers, and natural numbers in these definitions means that all the numbers used in factorials should not be decimals, negative numbers(-10 for example) or fractions.

******************************************************************

Natural numbers: The numbers that you use to count with: 1, 2, 3, 4…and on and on. Zero is not a natural number.

Whole numbers: All the numbers are ‘whole’ — there are no fractions, negative numbers or decimals. The only difference between whole and natural numbers is that 0 is a whole number.

Integers: Positive and negative whole numbers, for example -6, 0, -200,
1660, 2 etc.
******************************************************************

But what About “0!”

IS there a 0! Does it exists? Can we use it?
It has been agreed that zero factorial can be used and that 0! = 1.

I agree that it seems weird that multiplying no numbers together can give you one. Salman Khan from the Khan Academy explains the reasoning very well in this video.

Something interesting

We can calculate a factorial from the previous one. Have a look at this:

1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1= 6
4! = 4 * 3 * 2 * 1= 24
5! = 5 * 4 * 3 * 2 * 1= 120

Note that 3! (6) times 4 = 24, or the factorial of 4!
We continue in the same way and find that, 4! times 5 = 120, or 5!

And 6! is equal to 5! * 6 (120 * 6) = 720

So we can conclude that: n! = n × (n−1)!

The JavaScript function

Now for some JavaScript action! As always in coding, there is often more than one way to write a function. So let’s look at two options. The first one makes use of a recursion. A recursion means that a function is calling on itself.

function factorial (n) {
if (n === 0) { //remember that 0! is defined as 1
return 1;
}
return n * factorial(n — 1)//the recursion
}
factorial(3) //returns 6
factorial(0) //return 1
factorial(4) // returns 24

If this looks for you like a loop inside a function, you are absolutely right. We can also calculate the factorial of a number with a for loop:

function newFactorial (num) {
var result = 1;
for (var i = 2; i <= num; i++){ result *=i
}
return result
}
newFactorial(0)//return 1
newFactorial(3)//return 6
newFactorial(5)//return 120

We start our loop at 2 because 1 is already taken care of with var result = 1;

I encourage you to copy the code in the console and play with it a bit.

Real life use

Are factorials used in real life or are they just a mathematical exercise? Uh yes, factorials can be very useful. The number n! is the number of ways you can arrange n objects.

Let’s say that you have three dogs (Alpha, Beta, Charlie) and you want to see in how many ways you can arrange them in a line. 3! will give us 6 different ways. And if we write it out, we will find that it is indeed so:

   1. Alpha, Beta, Charlie
2. Alpha, Charlie, Beta
3. Beta, Alpha, Charlie
4. Beta, Charlie, Alpha
5. Charlie, Alpha, Beta
6. Charlie, Beta, Alpha

A deck of cards can be shuffled in 52! ways.

52!=80658175170943878571660636856403766975289505440883277824000000000000

As you can see, the numbers can get large very quickly.

I hoped that you have enjoyed learning about factorials. Next up are palindromes!

--

--

Marina Shemesh

My body may have left Africa but my soul does not agree. In Israel I have found love and the courage to do what I have always wanted to do: Write.