Algorithms

Lessons and articles on using algorithms

Factorial of a number in PHP

Hello, today we will tell you what is a factorial of the number and how it can be described using the language PHP. We have been studying this definition in school at mathematics classroom and it's not strange that we have to use it to write programs to calculate any required data.

So, we call factorial a multiplication of numbers from 1 before setting including the last. Sign which denotes the factorial is the exclamation mark (!). And the factorial of n is n! . Factorial formula for N looks like this:

n! = 1 * 2 * 3 * …. * n;

Here is an example of how to calculate factorial of 4:

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

Factorial is indicated only for the positive integers. Accepted, as we assume, that the factorial of 0 is 1.