搜索
首页后端开发php教程斐波那契数列 PHP

斐波那契数列 PHP

Aug 29, 2024 pm 01:12 PM
php

用外行的语言来说,斐波那契数列就是通过将前两个元素相加形成下一个元素,直到得到所需的数列大小而形成或获得的一系列元素。我们通常从 0 和 1 开始斐波那契数列。

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

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

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

系列一旦形成,如下:

0, 1, 1, 2 ,3, 5, 8, 13, 21, 34

如上所述,下一个数字是前两个数字相加而成的。

  • 上述序列中第 4 个 位置(第 n 位置)上的“2”是通过将其前面的两个数字相加获得的[ [n-1]和 n-2]), 1.
  • ‘3’是通过将其前面的两个数字 2 相加得到的。
  • ‘5’是通过将其前面的两个数字 3 相加得到的。
  • 等等。

PHP 中的斐波那契系列及其逻辑

在这里,我们将看到在 PHP 环境中工作时具体获取斐波那契数列。区别在于我们编码的格式,即使用 PHP 脚本的起始标签及其结束标签。

<?php …;
…;
…;
?>

这将帮助您理解和学习如何在 PHP 中使用迭代方式和递归方式两种方法生成斐波那契数列。

当给我们一个数字,即“n”(系列大小)时,我们将尝试找到不超过给定数字的斐波那契系列。

例如,如果我们需要为 n=5 创建斐波那契,我们将显示元素直到第 5 项。

示例#1

  • 输入:n = 9
  • 输出:0 1 1 2 3 5 8 13 21

示例 #2

  • 输入:n=13
  • 输出: 0 1 1 2 3 5 8 13 21 34 55 89 144

PHP 中的逻辑

逻辑与上述相同。这里我们给定 n=10,即我们需要找到直到第 n 项的元素。因此,我们将继续遵循我们的逻辑,直到我们的系列中有 n 个术语。

让我们看一下上面给出的示例之一。

在上面的一个示例中,我们有 n=9,逻辑表示:

  • 将第一个数字初始化为 0。
  • 将第二个数字初始化为 1。
  • 打印第一个和第二个数字。
  • 循环从这里开始。
  • 对于系列中的下一个元素,即第 3rd 元素 [nth 元素],我们将添加其紧邻的前两个数字 [(n-1) 和 (n- 2)] 获取该系列中的下一个数字,如下所示,0 + 1 = 1。

对于 n=3

  • n – 1 = 3 – 1 = 系列的第二个元素 = 1
  • n – 2 = 3 – 2 = 系列的第一个元素 = 0 3rd 元素 = (n-1) + (n-2) = 1 + 0 = 1

因此,级数中的第三个元素是 1。

  • 类似地,根据逻辑,要获得该系列的第 4 元素 [n],我们需要添加其前面的数字 e。 (n-1) 和 (n-2) 元素。

此时,‘n’等于‘4’:

  • n – 1 = 4 – 1 = 系列的第 3 个元素 = 1
  • n – 2 = 4 – 2 = 系列中的第 2 个元素 = 1 4 元素 = (n-1) + (n-2) = 1 + 1 = 2

因此,我们得到第 4 个 元素为 2。

因此,对于“n”等于 9,按照与上面解释相同的逻辑,我们得到序列为,斐波那契序列是 0 1 1 2 3 5 8 13 21

使用两种方法进行斐波那契打印的 PHP 系列

关于如何用 PHP 编写程序来打印斐波那契数列,基本上有两个著名的版本:

  • 没有递归
  • 使用递归

像 PHP 中一样,我们将使用“echo”语句来打印输出。

1.非递归方式

也称为使用迭代。在这种方法中,我们将从 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);
?>

代码说明:

  1. Here n is defined as equal to 10, so the logic will run till nth element e. Until we have n=10 elements in the series.
  2. First element is initialized to 0 and second element is initialized to 1, e. num1 = 0 and num2 = 1.
  3. The two elements i.e. num1 and num2 are printed as the first and second elements of the Fibonacci series.
  4. The logic we discussed will be used from here on and our iteration loop starts.
  5. According to our logic, to get num3, we need to add num1 and num2. Since currently num1 = 0 and num2 = 1, num3 comes out as 1.
  6. Now new number obtained is placed in num2 variable and num2 is saved in num1 variable. Basically simple swapping is taking place so that, now num1 is equal to ‘1’ and num2 = newly obtained num3 i.e. ‘1’.
  7. So when the next iteration happens and num3 is obtained by adding current values of num1 and num2, which, according to our PHP script, are as follows:
      • num1 = 1
      • num2 = 1
      • num3 = num1 + num2 = 1 + 1 = 2

Thus we get our next number in the Fibonacci Series.

  1. Similarly, the iteration keeps on till we achieve n = 10, size of the series that was defined in the program itself.

When the above program is executed, we get the output as follows:

斐波那契数列 PHP

2. The Recursion Way

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.

  1. We are using a For loop having a ‘counter’ variable that starts with 0. The For loop works up till the given ‘n’, size for the series.
  2. when loop starts, with the counter variable as 0, we use the recursion approach and call our defined function, Fibonacci ( ).
  3. Here code starts, with If and Else IF condition.
  4. First IF condition checks if ‘num’ variable holds value as ‘0’, if yes, then the code prints or returns ‘0’ as the element for the series.
  5. Similarly, second Else-If condition checks for the value ‘1’, if the ‘num’ variable holds ‘1’ as its value, the program returns it as it is.
  6. Next Else condition recursively calls the Fibonacci function for’ num’ values other than ‘0’ and ‘1’, continuing to reading the values from the For loop counter.

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.

斐波那契数列 PHP

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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP的当前状态:查看网络开发趋势PHP的当前状态:查看网络开发趋势Apr 13, 2025 am 12:20 AM

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP与其他语言:比较PHP与其他语言:比较Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP与Python:核心功能PHP与Python:核心功能Apr 13, 2025 am 12:16 AM

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP:网络开发的关键语言PHP:网络开发的关键语言Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP:许多网站的基础PHP:许多网站的基础Apr 13, 2025 am 12:07 AM

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

超越炒作:评估当今PHP的角色超越炒作:评估当今PHP的角色Apr 12, 2025 am 12:17 AM

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

PHP中的弱参考是什么?什么时候有用?PHP中的弱参考是什么?什么时候有用?Apr 12, 2025 am 12:13 AM

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

解释PHP中的__ Invoke Magic方法。解释PHP中的__ Invoke Magic方法。Apr 12, 2025 am 12:07 AM

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具