首页  >  文章  >  后端开发  >  PHP 中的阶乘

PHP 中的阶乘

王林
王林原创
2024-08-29 13:12:25852浏览

在开始学习 PHP 中的阶乘之前,让我们先了解一下阶乘这个术语。数字的阶乘是从 1 开始到数字本身的所有数字的乘积。在计算所有数字的乘积时,数字本身也包含在内。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

数字的阶乘仅针对正整数计算。 0 的阶乘始终为 1,负数的阶乘不存在。它由前面带有数字的“!”表示。示例 n!其中 n 是数字

所以,

5的阶乘!表示 5 的阶乘

7的阶乘!表示 7 的阶乘

例如,数字 5 的阶乘为:

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

同样,数字 7 的阶乘为:

7! = 7*6*5*4*3*2*1 = 5040

等等..

现在我们如何实际找到阶乘,我们可以使用

  1. for 循环(无递归)
  2. 使用递归

阶乘逻辑

获取数字阶乘的逻辑如下。

  1. 获取要计算阶乘的数。
  2. 获取从 1 到该数字的所有数字。
  3. 计算所有数字的乘积。

记住0的阶乘! = 1.

如何在 PHP 中求阶乘?

我们将进一步学习使用不同的方法使用 PHP 代码计算给定数字的阶乘。就像使用递归一样,有用户输入的递归,没有递归,没有用户输入的递归。

关于递归

与其他语言一样,PHP 也支持递归。什么是递归?当函数调用自身时称为递归。递归函数在函数内调用自身。

示例#1

在下面的 PHP 程序中计算数字 5 的阶乘。这是一个使用 for 循环的简单程序。这个 for 循环在从数字开始的数字序列上迭代,直到达到 1。

代码:

<?php
//example to calculate factorial of a number using simple for loop
//declaring the input number as 5
$input=5;
//declaring the fact variable as 1
$fact =1;
//iterating using for loop
for($i=$input; $i>=1;$i--) {
// multiply each number up to 5 by its previous consecutive number
$fact = $fact * $i;
}
// Print output of the program
echo '<br>'. 'The factorial of the number 5 is '. $fact
?>

输出:

PHP 中的阶乘

示例#2

在下面的程序中,我们使用了一个简单的 HTML 表单,其中包含输入文本和提交按钮。输入框用于获取用户输入。提交按钮用于提交表单数据。接下来是迭代 for 循环的 PHP 代码,其中存在我们在上一个程序中学到的所有逻辑。所以现在输入表单使用相同的逻辑。

如果用户通过表单中的输入框输入正数,则计算该数的阶乘并打印结果。

代码:

<html>
<head>
<title> Factorial Program</title>
</head>
<body>
<form method="POST">
<label>Enter a number</label>
<input type="text" name="number" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
// example to demonstrate factorial of a number using form
if($_POST['submit'] == "Submit") {
$input = $_POST['number'];
$fact=1;
//iterating using for loop
for($i=$input; $i>=1;$i--) {
$fact = $fact * $i;
}
// Print output of the program
echo '<br>'. 'The factorial of the number '.$input.' is ' . $fact;
}
?>
</body>
</html>

输出:

PHP 中的阶乘

示例#3

在上面的两个程序中,我们没有将逻辑包装在函数中。这里我们将主要逻辑封装在一个函数中,然后调用该函数来计算 PHP 中给定数字的阶乘。这里函数的名称是 Factorial_Function,它求数字 8 的阶乘。

代码:

//example to calculate factorial of a number using function
//defining the factorial function
function Factorial_Function($number) {
$input = $number;
$fact=1;
//iterating using for loop
for($i=$input; $i>=1;$i--) {
$fact = $fact * $i;
}
return $fact;
}
//calling the factorial function
$result = Factorial_Function(8);
echo 'Factorial of the number 8 is '.$result;
?>

输出 :

PHP 中的阶乘

示例#4

我们知道递归就是在函数内调用函数。在下面的示例中,我们将使用递归并使用 PHP 代码查找数字的阶乘。主要逻辑包含在函数名称 Factorial_Function 中。在此函数中,如果输入大于 1,则再次调用相同的函数,如果输入小于或等于 1,则返回 1。

使用递归

代码:

<?php
//Example to demonstrate factorial of a number using recursion
//function containing logic of factorial
function Factorial_Function($input)
{
// if the input is less than or equal to 1 then return
if($input <=1) {
return 1;
}
// else do a recursive call and continue to find the factorial
return $input * Factorial_Function($input-1);  //doing a recursive call
}
echo "Factorial of 9 is ".Factorial_Function(9);
?>

输出:

PHP 中的阶乘

示例#5

我们现在已经了解了递归。在下面的程序中,我们使用了递归,递归应用于本示例中用户输入的数字。

代码:

<html>
<head>
<title> Factorial Program</title>
</head>
<body>
<form method="POST">
<label>Enter a number</label>
<input type="text" name="number" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
// example to demonstrate factorial of a number using form
function Factorial_Function($input)
{
// if the input is less than or equal to 1 then return
if($input <=1) {
return 1;
}
// else do a recursive call and continue to find the factorial
return $input * Factorial_Function($input-1); //doing a recursive call
}
if(!empty($_POST['number'])){
$input = $_POST['number'];
// Print output of the program
echo '<br>'. 'The factorial of the number '.$input.' is ' . Factorial_Function($input);
}
?>
</body>
</html>

输出:

PHP 中的阶乘

结论

本文涵盖了使用 PHP 求数字阶乘的所有解释和示例。使用递归和非递归方式解释示例,并结合程序上下文进行递归解释。希望这篇文章能为您提供有益的学习和掌握。

以上是PHP 中的阶乘的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn