-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimesGenerator.php
119 lines (91 loc) · 3.24 KB
/
PrimesGenerator.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
110
111
112
113
114
115
116
117
118
119
<?php
require_once 'Base.php';
class PrimesGenerator extends Base
{
private $count;
private $timeout;
private $redis;
private $primeNum5k = 0;
private $result = 0;
private $prev = 0;
private $sum = 0;
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->primeNumber();
}
public function primeNumber()
{
echo "Prime numbers Generator started...\n";
$number = 2;
$loop = 1;
while ($loop <= $this->count) {
for ($i = 1; $i <= $number; $i++) {
if ($i != 2 && $i > 1 && $number / $i != 1 && $number % $i == 0) {
break;
}
if ($number / $i == 1 && $number % $i == 0) {
$this->result = $number;
$this->sum = bcadd($this->result, $this->prev);
echo "loop num:\t{$loop}\n";
echo "result:\t\t{$this->result}\n\n";
$this->prev = $this->result;
$this->redis->rpush('prime', $number);
$loop++;
}
}
if ($number < 3) $number++;
else $number += 2;
usleep($this->timeout * 1000);
}
echo "\n";
echo 'count: '.$this->count."\n";
echo 'timeout: '.$this->timeout."\n";
}
public function primeNumberEratosthenes()
{
echo "Prime numbers Generator started...\n";
$numbers = range(2, $this->count);
foreach ($numbers as $key => $number) {
if ($number != 2 && $number % 2 == 0) unset($numbers[$key]);
}
$next_prime = 3;
// Sieve of Eratosthenes
while ($next_prime <= sqrt($this->count)) {
foreach ($numbers as $key => $number) {
if ($number != 2 && $number % $next_prime == 0 && $number / $next_prime != 1) unset($numbers[$key]);
}
$next_prime += 2;
}
$numbers = array_values($numbers);
$i = 1;
foreach ($numbers as $key => $number) {
$this->result = $number;
$this->sum = bcadd($this->result, $this->prev);
echo "loop num:\t{$i}\n";
echo "previous:\t{$this->prev}\n";
echo "result:\t\t{$this->result}\n";
echo "sum:\t\t{$this->sum}\n";
echo "digits num:\t".strlen($number)."\n\n";
$this->prev = $this->result;
$this->redis->rpush('prime', $number);
usleep($this->timeout * 1000);
$i++;
}
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 PrimesGenerator((int)$options['c'], (int)$options['t']);