How to calculate the factorial of a given number n through a PHP program

青灯夜游
Release: 2023-03-12 06:44:01
Original
8074 people have browsed it

In the previous article "PHP Loop Learning 9: Obtaining the greatest common divisor between given two numbers", we introduced the use of while loop statements to find the greatest common divisor between given two integers. Method, this time we will talk about factorial and introduce how to calculate the factorial of a given integer N. Interested friends can learn about it~

First of all, let’s understandWhat is factorial?

The factorial of a positive integer is the product of all positive integers less than and equal to the number. Therefore, the factorial of a given integer N is:

1 × 2 × 3×...× (n-1) × n

Finding the factorial is simple , is a programming problem that students who are just starting to program will definitely encounter, and there are many ways to implement it. This article will introduce you to various methods of implementing factorial from the perspective of for loop, while loop, do-while loop, and recursion.

Method 1: Use a for loop to implement the factorial of N

Implementation idea:

  • Because finding the factorial of n is Find the product of 1 times 2 times 3...all the way up to n. Therefore, the initial condition of the for loop can be set to i = 1, and the restriction condition can be i or <code>i . <br>

  • Then the loop body is the multiplication operation. Multiply the i value of each loop to get a product

  • Finally output the product

Let’s take a look at the implementation method:

<?php
header("Content-type:text/html;charset=utf-8");
//第一种方法--for循环
function Factorial($n) {
	$sum = 1;
	for ($i = 1; $i <= $n; $i++) {
		$sum *= $i;
	}
	echo "$n 的阶乘为: " . $sum."<br><br>";
}

Factorial(5);
Factorial(10);
Factorial(100);
?>
Copy after login

$sum *= $iThe statement is equivalent to $sum=$sum*$i, *=The assignment operator can multiply the variable on the left side of the operator by the value of the expression on the right side and assign it to the variable on the left. Note: Since any value multiplied by 0 is 0, the initial value of the variable $sum must be 1.

Let’s take a look at the output results:

How to calculate the factorial of a given number n through a PHP program

#We understand how the for loop implements the factorial of N. The implementation methods of the while loop and do-while loop are also That’s almost all I know (their implementation thinking is the same).

Method 2: Use a while loop to implement the factorial of N

<?php
header("Content-type:text/html;charset=utf-8");
//第二种方法--while循环
function Factorial($n) {
	$i = 1;
	$sum=1;
	while($i<=$n){
	    $sum*=$i;
	    $i++;
	}
	echo "$n 的阶乘为: " . $sum."<br><br>";
}

Factorial(1);
Factorial(2);
Factorial(3);
Factorial(11);
?>
Copy after login

Output result:

How to calculate the factorial of a given number n through a PHP program

Method 3: Use do-while loop to implement the factorial of N

<?php
header("Content-type:text/html;charset=utf-8");
//第三种方法--do while循环
function Factorial($n) {
	$i = 1;
	$sum=1;
	do {
        $sum *= $i;
        $i++;
    } while ($i <= $n);
	echo "$n 的阶乘为: " . $sum."<br><br>";
}

Factorial(2);
Factorial(3);
Factorial(4);
Factorial(10);
?>
Copy after login

Output result:

How to calculate the factorial of a given number n through a PHP program

Compare method 1, method 2 and method 3. Did you find that they are very similar!

Okay, here comes another important point. Let’s take a look at how recursion implements the factorial of N.

Method 4: Use recursion to implement the factorial of N

So what is recursion? Simply put, recursion means that the program calls itself and the function keeps referencing itself until the referenced object is known. The following two conditions must be met to constitute recursion:

  • The subproblem must be the same thing as the original problem and simpler.

  • You cannot call yourself without limit, there must be an exit, and it can be simplified to non-recursive situation processing.

According to recursive thinking, factorial can be expressed by the following formula:

f(1) = 1
f(n) = n * f(n-1) {n>1}
Copy after login

Therefore, the code to use PHP to recursively find N factorial is as follows:

<?php
header("Content-type:text/html;charset=utf-8");
//第四种方法--递归
function Factorial($n) {
	$sum=1;
	if($n <= 1){
		return 1;
	}else{
		$sum = $n * factorial($n-1);
		return $sum;
	}
}


echo "2 的阶乘为: " .Factorial(2)."<br><br>";
echo "3 的阶乘为: " .Factorial(3)."<br><br>";
echo "4 的阶乘为: " .Factorial(4)."<br><br>";
echo "10 的阶乘为: " .Factorial(10);
?>
Copy after login

Output result:

How to calculate the factorial of a given number n through a PHP program

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

Recommended: PHP interview questions summary (collection)

The above is the detailed content of How to calculate the factorial of a given number n through a PHP program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!