Fibonacci Numbers for app.codility.com/programmers Explained in PHP
Category of Article : Technology | Computers and Software | Algorithm
By: Chrysanthus Date Published: 30 Sep 2025
Fibonacci numbers are a sequence of integers. The first two of these numbers are 0 and 1. The next number is formed by adding the previous two numbers. That is, each subsequent number is the sum of the previous two. The first sixteen Fibonacci numbers are:
|
Nos. |
0 |
1 |
1 |
2 |
3 |
5 |
8 |
13 |
21 |
34 |
55 |
89 |
144 |
233 |
377 |
610 |
|
Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
By definition, this is done recursively. In symbolic form, Fibonacci numbers are defined as follows:
|
|
{ |
0 |
for n = 0, |
|
Fn = |
1 |
for n = 1, |
|
|
|
Fn-1 + Fn-2 |
for n > 1 |
where n is the index.
Implementation
The code is:
<?php
function fibonacciDynamic($n) {
if ($n == 0)
return [0];
if ($n == 1)
return [0, 1];
$noOperations = 2;
$v = [0, 1];
for ($i=2; $i < $n; $i++) {
$nextNo = $v[$i - 1] + $v[$i - 2];
$v[] = $nextNo; //push_back
$noOperations++;
}
echo "No. of operations: " . $noOperations . "\n";
return $v;
}
$vtr = fibonacciDynamic(16);
echo "Fibonacci numbers: ";
for ($i=0; $i < count($vtr); $i++) {
echo $vtr[$i] . ', ';
}
echo "\n";
?>
The output is:
No. of operations: 16
Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
Enumeration of the Fibonacci numbers has been done, by simply using a basis of dynamic programming. The values F2 , F3 , . . . , Fn have been calculated based on the previously calculated two numbers (it is sufficient to remember only the first two values).
Fast Algorithm for a particular Fibonacci Number
With the above approach, in order to get the Fibonacci number corresponding to a particular index, all the previous Fibonacci numbers have to be obtained first. For example, in order to obtain the Fibonacci number, 13 corresponding to the index, 7, the previous Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8 have to be obtained first. The following formula can be used to produce a particular Fibonacci number, when given the corresponding zero based index:
In the formula, each of the bracketed term is raised to the power n, and it is not √5 that is raised to the power n. Fn is a Fibonacci number such as 13, corresponding to the zero based index, n = 7. A PHP statement to implement the above formula for n = 50001 is:
F50001 = (Math.pow(((1+Math.sqrt(5))/2), 50001) - Math.pow(((1-Math.sqrt(5))/2), 50001) )/Math.sqrt(5);
This means the math.h library has to be included for the power function, pow() and the square root function, sqrt(). The reader should avoid using this formula whenever he/she can. This is because on the right hand side, double or float numbers are required while on the left hand side, a long integer is required.
Conclusion
In symbolic form, Fibonacci numbers are defined as follows:
|
|
{ |
0 |
for n = 0, |
|
Fn = |
1 |
for n = 1, |
|
|
|
Fn-1 + Fn-2 |
for n > 1 |
They can be coded like that in O(n) time. To produce one Fibonacci number, use the formula:
The rest of the 17 lessons, with explanation of lessons, explanation of tasks, and explanation of solutions, can be found at (click): Explanations in PHP for the 17 Algorithm Lessons at https://app.codility.com/programmers with Problems and Solutions
Related Links
Basics of PHP with Security ConsiderationsWhite Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links