-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiccanobif.php
More file actions
30 lines (27 loc) · 812 Bytes
/
iccanobif.php
File metadata and controls
30 lines (27 loc) · 812 Bytes
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
<?php
// 7 kyu - The iccanobiF Sequence
// Your task is to create an array of size n with the values of the Fibonnaci sequence, and reverse the order in which the sequence is displayed.
//
// For example: [1, 1, 2, 3, 5] would become [5, 3, 2, 1, 1]
//
// You can assume that n will always be a positive integer between and including, 1 and 64.
function iccanobif($n) {
if ($n === 1) return [1];
$arr = [1,1];
for($i = 0; $i < $n - 2; $i++){
$arr[] = $arr[$i] + $arr[$i + 1];
}
return array_reverse($arr);
}
// Alternative Solution:
// function fibonacci($n) {
// if ($n < 3) return 1;
// return fibonacci($n - 2) + fibonacci($n - 1);
// }
//
// function iccanobif($n) {
// return array_reverse(array_map('fibonacci', range(1, $n)));
// }
$answer = iccanobif(1);
print_r("$answer \n");
?>