-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacciGenerator.php
70 lines (54 loc) · 1.81 KB
/
FibonacciGenerator.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
<?php
require_once 'Base.php';
class FibonacciGenerator extends Base
{
private $count;
private $timeout;
private $cur = '0';
private $prev = '0';
private $result;
private $fibonacciNum2k;
private $redis;
public function __construct(int $count, int $timeout)
{
parent::__construct();
if ($count < 1) exit("parameter \"-c\" should be integer and greater than 0\n");
$this->count = $count;
if ($timeout < 1) exit("parameter \"-t\" should be integer and greater than 0\n");
$this->timeout = $timeout;
$this->redis = $this->getRedis();
$this->fibonacciNumber();
}
public function fibonacciNumber()
{
echo "Fibonacci numbers Generator started...\n";
for ($i = 0; $i < $this->count; $i++) {
if ($i == 0) {
$this->result = '0';
} elseif ($i == '1' || $i == '2') {
$this->cur = '1';
$this->prev = '0';
} else {
$this->prev = $this->cur;
$this->cur = $this->result;
}
$this->result = bcadd($this->cur, $this->prev);
echo "loop num:\t".($i + 1)."\n";
echo "current:\t{$this->cur}\n";
echo "previous:\t{$this->prev}\n";
echo "result:\t\t{$this->result}\n\n";
$this->redis->rpush('fib', $this->result);
usleep($this->timeout * 1000);
}
echo "\n";
echo 'count: '.$this->count."\n";
echo 'timeout: '.$this->timeout."\n";
}
}
// "c" stands for Count
// "t" stands for Timeout
$options = getopt('c:t:');
if (!isset($options['c']) || !isset($options['t'])) {
exit("Usage: php <filename> -c [int] -t [int]\n");
}
$generator = new FibonacciGenerator((int)$options['c'], (int)$options['t']);