-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacchi.php
More file actions
64 lines (53 loc) · 1.39 KB
/
fibonacchi.php
File metadata and controls
64 lines (53 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
// fibonachhi number line,
// takes current number adds to next number in the sequence
// non-recursive way of fibonacci sequence.
// how many times will repeat nth times.
$number = 20;
// call to function. with amount of times to call.
Fib($number);
// function.
function Fib($number)
{
// initial numbers and counter number for repetition.
$number_1 = 0;
$number_2 = 1;
$counter = 0;
// while loop, counter to $number variable.
while($counter < $number) {
// echo first number.
echo ', ' . $number_1;
// add first and second number together.
$number_3 = $number_2 + $number_1;
// second number reassigned to first number.
$number_1 = $number_2;
// new 3rd number assigned to second number.
$number_2 = $number_3;
// counter increases
$counter = $counter+1;
}
echo "<br>";
echo "<br>";
}
// Recursive way Fibonacchi sequence
// number of repetitions.
$num = 20;
// for, for recursion.
for ($count = 0; $count < $num; $count++)
{
// echo out Fibonacchi function result.
echo Fibonacchi($count) . ', ';
}
// recursive Fibonacchi function.
function Fibonacchi($num)
{
if ($num == 0) {
// $num == 0;
return 0;
} else if ($num == 1) {
// $num == 1;
return 1;
} else {
return (Fibonacchi($num - 1) + Fibonacchi($num - 2));
}
}