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.

You can define the factorial using recursive and cyclic method. Recursion, remember, called the process in which repeated elements follow in an arbitrary manner. Example of recursion, of course in the first place, you can call the factorial of a number. The idea of it is that it creates a function which has a call of itself and a condition that is that if we check n <= 1, then 1 is returned, and if not - then the parameter n is multiplied by the function factorial (n - 1).

Consider how to make a recursive method to calculate the factorial for PHP. To do this, first of all, we need a text editor in which we write our code, as the creation of the program requires to save to a file with extension .php to a folder on your server and run the Internet browser to open the file. <?php function factorial ($n) { if($n <= 1) { return 1; } else { return $n * factorial($n - 1); } }

echo factorial(4); ?>

Thus, by using the echo factorial(4); in our file and running it in our browser, we get the number 24, which is the factorial of 4.

If you use the cyclic method of calculating factorial, then it will be a simple multiplication of numbers from 1 to the end of the given number. Consider this example in PHP language . Create a new document in a text editor and write the code. After writing code. do the same steps as in the previous example, but with the recursive method. <?php function factLoop($n) { $result = 1;

for ($index = 2; $index <= $n; $index++) { $result = $result * $index; }

return $result; }

echo factLoop(4); ?> >

Here we can see that we first announced factLoop function with parameter $n. Then the variable $result is set to 1 and starts the cycle in which the condition set by the variable $index is 2 and compared with the number $n, then at the end of each cycle parameter $index increased by 1. If the condition is true, then multiplication parameter $result is multiplied by $index. When the cycle ends for number 4, you also see the value 24 on the screen of your browser .

To summarize, with cyclical and recursive methods for calculating the factorial you can use the one that fits into your task, or liked by you personally. If the numbers are large, program that uses a cyclic method will work faster. Recursive method uses more RAM. which is not very suitable when you have small amount of memory. The advantage of recursion is the fact that it more visually clear than cyclical. Thus, see your self what is preferred for you!

Best regards, DYAMAR Engineering.

Language: 
Category: 

All rights reserved. You may not copy or distribute, electronically or otherwise, any part of this article for any purpose without prior written permission from the DYAMAR Engineering.