Skip to content

Commit 9c688c1

Browse files
committed
Added some more examples of recursion
1 parent 1239f20 commit 9c688c1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

chap-05/recursion-explained.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,17 @@
5151

5252

5353
$n = 10;
54+
$x = 110;
5455

5556
echo "The factor of {$n} is : " .factorial($n) . PHP_EOL;
5657

58+
echo "The Fibonacci number of {$n} is : " . fibonnacci($n) . PHP_EOL;
59+
60+
echo "The Greatest Common Division of {$n} and {$x} is : " . gcd($n, $x) . PHP_EOL ;
61+
5762
echo "Be sure to check the docblock of this file for more explanation!" . PHP_EOL;
5863

64+
5965
function factorial(int $n) : int
6066
{
6167

@@ -68,3 +74,23 @@ function factorial(int $n) : int
6874
return $n * factorial($n - 1);
6975
}
7076

77+
function fibonnacci(int $n) : int
78+
{
79+
if ($n == 0){
80+
return 1;
81+
} else if ($n == 1) {
82+
return 1;
83+
} else {
84+
return fibonnacci($n - 1) + fibonnacci($n - 2);
85+
}
86+
}
87+
88+
function gcd(int $a, int $b) : int
89+
{
90+
if ($b == 0) {
91+
return $a;
92+
} else {
93+
return gcd($b, $a % $b);
94+
}
95+
}
96+

0 commit comments

Comments
 (0)