用外行的语言来说,斐波那契数列就是通过将前两个元素相加形成下一个元素,直到得到所需的数列大小而形成或获得的一系列元素。我们通常从 0 和 1 开始斐波那契数列。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
系列一旦形成,如下:
0, 1, 1, 2 ,3, 5, 8, 13, 21, 34
如上所述,下一个数字是前两个数字相加而成的。
在这里,我们将看到在 PHP 环境中工作时具体获取斐波那契数列。区别在于我们编码的格式,即使用 PHP 脚本的起始标签及其结束标签。
<?php …; …; …; ?>
这将帮助您理解和学习如何在 PHP 中使用迭代方式和递归方式两种方法生成斐波那契数列。
当给我们一个数字,即“n”(系列大小)时,我们将尝试找到不超过给定数字的斐波那契系列。
例如,如果我们需要为 n=5 创建斐波那契,我们将显示元素直到第 5 项。
示例#1
示例 #2
逻辑与上述相同。这里我们给定 n=10,即我们需要找到直到第 n 项的元素。因此,我们将继续遵循我们的逻辑,直到我们的系列中有 n 个术语。
让我们看一下上面给出的示例之一。
在上面的一个示例中,我们有 n=9,逻辑表示:
对于 n=3
因此,级数中的第三个元素是 1。
此时,‘n’等于‘4’:
因此,我们得到第 4 个第 元素为 2。
因此,对于“n”等于 9,按照与上面解释相同的逻辑,我们得到序列为,斐波那契序列是 0 1 1 2 3 5 8 13 21
关于如何用 PHP 编写程序来打印斐波那契数列,基本上有两个著名的版本:
像 PHP 中一样,我们将使用“echo”语句来打印输出。
也称为使用迭代。在这种方法中,我们将从 0 和 1 开始序列。之后我们将打印第一个和第二个数字。接下来我们将使用循环开始迭代,这里我们使用 while 循环。
用于打印前 10 个斐波那契数列元素的 PHP 脚本。
代码:
<?php function Fibonacci($n) { $num1= 0; $num2= 1; $counter= 0; while($counter < $n) { echo ' '.$num1; $num3= $num2 + $num1; $num1= $num2; $num2= $num3; $counter= $counter+1; } } //for a pre defined number for Fibonacci. $n=10; Fibonacci($n); ?>
代码说明:
Thus we get our next number in the Fibonacci Series.
When the above program is executed, we get the output as follows:
By recursion, we mean the way where the same function is called repeatedly until a base condition is achieved or matched. At this point, recursion is stopped.
The said “function is called repeatedly” phrase points to the section in your code where we will define our logic for the Fibonacci Series.
Below is an example of generating Fibonacci Series in PHP, using If-Else conditions giving way for our recursive approach.
Here is the PHP Scripts for printing the first 15 elements for Fibonacci Series.
<?php function Fibonacci($num) { //If-Else IF will generate first two numbers for the series if($num == 0) return 0; else if($num == 1) return 1; // This is where Recursive way comes in. //recursive call to get the rest of the numbers in the series else return(Fibonacci($num -1) + Fibonacci( $num -2)); } //For a given n=15 $num =15; for($counter = 0; $counter < $num; $counter++) { echo Fibonacci($counter).' '; } ?>
Code Explanation:
This is the recursive way, which means our function that contains our logic is called again and again for generating the next element in the series until our condition for achieving a specific series size is obtained.
In Iterative approaches, the First and Second element is first initialized and printed. Here we allow a For Loop to give us our first and second elements starting with 0 and 1.
This is where our Fibonacci Logic comes into work and the next number in the sequence is obtained by adding its previous two numbers. Because this is the recursive method, we need to give a counter value to count the recursions equal to nth value, which is being handled by our For Loop.
When the above program or code is executed, the following output is displayed.
The Fibonacci Series does not only appear in mathematics or science calculations but in nature too, have you ever noticed Yellow chamomile flower head.
The Fibonacci Series if plotted on a graph, it forms a spiral called Fibonacci Spiral. It is also one of the gems given by Indian soil. It is found in Indian Mathematics as early as 200 BC in the works done by the mathematician, Pingala. Later Fibonacci introduced the sequence to European countries in his book Liber Abacci in 1200s.
以上是斐波那契数列 PHP的详细内容。更多信息请关注PHP中文网其他相关文章!