Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP Siri Fibonacci

PHP Siri Fibonacci

王林
王林asal
2024-08-29 13:12:20420semak imbas

Jika dikatakan dalam bahasa awam, Siri Fibonacci ialah satu siri elemen yang terbentuk atau diperoleh, apabila dua elemen sebelumnya ditambah untuk membentuk elemen seterusnya sehingga kita mendapat saiz siri yang diperlukan. Kami biasanya memulakan Siri Fibonacci dengan 0 dan 1.

IKLAN Kursus Popular dalam kategori ini PEMBANGUN PHP - Pengkhususan | 8 Siri Kursus | 3 Ujian Olok-olok

Mulakan Kursus Pembangunan Perisian Percuma Anda

Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain

Siri apabila terbentuk, muncul seperti di bawah:

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

Seperti yang dinyatakan di atas, nombor seterusnya diperoleh dengan menjumlahkan dua nombor sebelumnya.

  • '2' pada kedudukan ke-4ke (kedudukan keke) dalam siri yang diberikan di atas, diperoleh dengan menambah dua nombor sebelumnya[ [n-1] dan n-2]), 1.
  • ‘3’ diperoleh dengan menambah dua nombor sebelumnya,2.
  • ‘5’ diperoleh dengan menambah dua nombor sebelumnya,3.
  • Dan seterusnya.

Siri Fibonacci dalam PHP dan Logik

Di sini kita akan melihat secara khusus mendapatkan Siri Fibonacci semasa kita bekerja dalam persekitaran PHP. Perbezaannya ialah format di mana kita akan mengekod, iaitu menggunakan teg permulaan untuk skrip PHP dan teg penamatnya.

<?php
…;
…;
…;
?>

Ini akan membantu anda memahami dan mempelajari cara siri Fibonacci ini dijana dalam PHP menggunakan dua kaedah iaitu cara Iteratif dan cara Rekursif.

Apabila kami diberi nombor iaitu ‘n’ iaitu saiz siri, kami akan cuba mencari Siri Fibonacci sehingga nombor yang diberikan.

Sebagai contoh, jika kami dikehendaki mencipta Fibonacci untuk n=5, kami akan memaparkan elemen sehingga penggal ke-5.

Contoh #1

  • Input: n = 9
  • Output: 0 1 1 2 3 5 8 13 21

Contoh #2

  • Input: n=13
  • Output: 0 1 1 2 3 5 8 13 21 34 55 89 144

Logik dalam PHP

Logik adalah sama seperti yang dinyatakan di atas. Di sini kita telah memberikan n=10, iaitu kita perlu mencari unsur-unsur sehingga sebutan ke-n. Oleh itu, kami akan terus mengikut logik kami sehingga kami mempunyai n istilah dalam siri kami.

Mari kita lihat salah satu contoh yang diberikan di atas.

Dalam salah satu contoh di atas kita mempunyai n=9 dan Logik mengatakan bahawa:

  • Mulakan nombor pertama sebagai 0.
  • Awalkan nombor kedua sebagai 1.
  • Cetak nombor pertama dan kedua.
  • Gelung bermula di sini.
  • Untuk elemen seterusnya dalam siri ini iaitu elemen 3rd [nelemen ke], kami akan menambah dua nombor yang terdekat sebelum itu [(n-1) dan (n- 2)] untuk mendapatkan nombor seterusnya dalam siri ini, seperti di sini, 0 + 1 = 1.

Untuk n=3

  • n – 1 = 3 – 1 = elemen ke-2 siri = 1
  • n – 2 = 3 – 2 = elemen pertama siri = 0 3rd elemen = (n-1) + (n-2) = 1 + 0 = 1

Oleh itu, elemen ketiga dalam siri ini ialah 1.

  • Begitu juga mengikut logik, untuk mendapatkan elemen ke-4ke [n] siri itu kita perlu menambah nombor sebelumnya e. (n-1) dan (n-2) elemen.

Kini pada ketika ini, 'n' bersamaan dengan '4':

  • n – 1 = 4 – 1 = elemen ke-3 siri = 1
  • n – 2 = 4 – 2 = elemen ke-2 siri = 1 4elemen ke = (n-1) + (n-2) = 1 + 1 = 2

Oleh itu, kami mendapat elemen ke-4ke kami sebagai 2.

Oleh itu, untuk 'n' bersamaan dengan 9, mengikut logik yang sama seperti yang dijelaskan di atas kita mendapat jujukan sebagai, jujukan Fibonacci ialah 0 1 1 2 3 5 8 13 21

Siri PHP untuk Percetakan Fibonacci dengan Dua Pendekatan

Pada asasnya terdapat dua versi terkenal tentang cara kita boleh menulis program dalam PHP untuk mencetak Siri Fibonacci:

  • Tanpa Rekursi
  • Dengan Rekursi

Seperti biasa dalam PHP, kami akan menggunakan pernyataan ‘gema’ untuk mencetak output.

1. Cara Bukan Rekursi

Juga dikenali sebagai menggunakan Lelaran. Ini adalah pendekatan di mana kita akan memulakan siri dengan 0 dan 1. Selepas itu kita akan mencetak nombor pertama dan kedua. Selepas itu kita akan mulakan dengan lelaran kita menggunakan gelung, di sini kita menggunakan gelung sementara.

Skrip PHP untuk mencetak 10 elemen Siri Fibonacci yang pertama.

Kod:

<?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);
?>

Penjelasan Kod:

  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 Siri Fibonacci

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 Siri Fibonacci

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.

Atas ialah kandungan terperinci PHP Siri Fibonacci. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Bertukar dalam PHPArtikel seterusnya:Bertukar dalam PHP