forked from btcmp/bootcamp186
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeretAngka.php
109 lines (91 loc) · 1.87 KB
/
DeretAngka.php
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
class DeretAngka{
//VOID
//RETURN
// some change
//0 2 4 6 4 2 0
public function getOddAndReverse($n){
$result = array();
$data = 0;
for($i = 0; $i < $n; $i++){
//collect to result array
$result[$i] = $data;
if($i <= ($n-1)/2-1){
$data = $data + 2;
} else {
$data = $data - 2;
}
}
return $result;
}
//single return
public function getTambah($n1, $n2){ //parameter
$hasil = $n1 + $n2;
//echo $hasil;
return $hasil;
}
//array return / multidata
//0,1,2,3,4,5
public function getIncrement($n){
$hasil = array();
for($i = 0; $i < $n; $i++){
$hasil[$i] = $i;
}
return $hasil;
}
//1,3,5..
public function getIncrementBy2($n){
$hasil = array();
$data = 1;
for($i = 0; $i < $n; $i++){
$hasil[$i] = $data;
$data = $data + 2;
}
return $hasil;
}
//5,3,1...
public function getIncrementBy2Reverse($n){
$hasil = array();
$data = 1;
for($i = 0; $i < $n; $i++){
$hasil[($n-$i-1)] = $data;
$data = $data + 2;
}
return $hasil;
}
//1,1,2,3,5...
public function getFibo($n){
$result = array();
$result[0] = 1;
$result[1] = 1;
for($i = 2; $i < $n; $i++){
$result[$i] = $result[$i-1] + $result[$i-2];
}
return $result;
}
public function getChars($n){
$result = array();
$char = "A";
for($i = 0; $i < $n; $i++){
$result[$i] = $char;
$char++;
}
return $result;
}
}
//test
//$deret=new DeretAngka();
//$result = $deret->getTambah(12, 13); //argument
//echo $result;
//echo json_encode($deret->getIncrement(9));
//echo "<br/>" . json_encode($deret->getIncrementBy2(9));
//echo "<br/>" . json_encode($deret->getIncrementBy2Reverse(9));
//echo "<br/>" . json_encode($deret->getFibo(9));
/*
$result1 = $deret->getIncrementBy2(9);
$result2 = $deret->getIncrementBy2Reverse(9);
echo "<br/>";
for($i = 0; $i < 9; $i++){
echo $result2[$i] . "<br/>";
}
*/