PHP 中的階乘

王林
發布: 2024-08-29 13:12:25
原創
851 人瀏覽過

在開始學習 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中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!