Completed
Pull Request — master (#1)
by Pol
04:26 queued 01:57
created

PrimeSieveOfEratosthenes::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class PrimeSieveOfEratosthenes.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class PrimeSieveOfEratosthenes extends Combinatorics implements \Iterator, \Countable {
13
14
  /**
15
   * The minimum limit.
16
   *
17
   * @var int
18
   */
19
  protected $min;
20
21
  /**
22
   * The maximum limit.
23
   *
24
   * @var int
25
   */
26
  protected $max;
27
28
  /**
29
   * The key.
30
   *
31
   * @var int
32
   */
33
  protected $key;
34
35
  /**
36
   * The primes array.
37
   *
38
   * @var array
39
   */
40
  private $primes;
41
42
  /**
43
   * @var int
44
   */
45
  private $last;
46
47
  /**
48
   * PrimeSieveOfEratosthenes constructor.
49
   */
50
  public function __construct() {
51
    $this->setMaxLimit(PHP_INT_MAX);
52
    $this->setMinLimit(0);
53
    $this->primes = array(2, 3);
54
  }
55
56
  /**
57
   *
58
   */
59
  private function generateNextPrime() {
60
    $candidate = end($this->primes);
61
62
    do {
63
      $candidate += 2;
64
      $isPrime = TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
65
      foreach ($this->primes as $prime) {
66
        if ($candidate % $prime == 0) {
67
          $isPrime = FALSE;
68
          break;
69
        }
70
        if ($prime * $prime > $candidate) {
71
          break;
72
        }
73
      }
74
    } while (!$isPrime);
75
76
    $this->primes[] = $candidate;
77
  }
78
79
  /**
80
   * {@inheritdoc}
81
   */
82
  public function rewind() {
83
    $this->key = 0;
84
  }
85
86
  /**
87
   * {@inheritdoc}
88
   */
89
  public function current() {
90
    if (!isset($this->primes[$this->key()])) {
91
      $this->generateNextPrime();
92
    }
93
94
    $this->last = $this->primes[$this->key()];
95
96
    return $this->last;
97
  }
98
99
  /**
100
   * {@inheritdoc}
101
   */
102
  public function valid() {
103
    return $this->last < $this->getMaxLimit();
104
  }
105
106
  /**
107
   * {@inheritdoc}
108
   */
109
  public function key() {
110
    return $this->key;
111
  }
112
113
  /**
114
   * {@inheritdoc}
115
   */
116
  public function next() {
117
    ++$this->key;
118
  }
119
120
  /**
121
   * Count elements of an object.
122
   *
123
   * @return int
124
   *   The number of element.
125
   */
126
  public function count() {
127
    return count($this->toArray());
128
  }
129
130
  /**
131
   * Convert the iterator into an array.
132
   *
133
   * @return array
134
   *   The elements.
135
   */
136
  public function toArray() {
137
    $data = array();
138
139
    for ($this->rewind(); $this->valid(); $this->next()) {
140
      $data[] = $this->current();
141
    }
142
143
    return $data;
144
  }
145
146
  /**
147
   * Set the maximum limit.
148
   *
149
   * @param int $max
150
   *   The limit.
151
   */
152
  public function setMaxLimit($max) {
153
    $this->max = $max;
154
  }
155
156
  /**
157
   * Get the maximum limit.
158
   *
159
   * @return int
160
   *   The limit.
161
   */
162
  public function getMaxLimit() {
163
    return intval($this->max);
164
  }
165
166
  /**
167
   * Set the minimum limit.
168
   *
169
   * @param int $min
170
   *   The limit.
171
   */
172
  public function setMinLimit($min) {
173
    $this->min = $min;
174
  }
175
176
  /**
177
   * Get the minimum limit.
178
   *
179
   * @return int
180
   *   The limit.
181
   */
182
  public function getMinLimit() {
183
    return $this->min <= 2 ? 2 : intval($this->min);
184
  }
185
186
  /**
187
   * Get a rough estimation of how many prime numbers there are.
188
   *
189
   * @return float
190
   *   The number of primes.
191
   */
192
  public function pi() {
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
193
    return $this->getMaxLimit() / log($this->getMaxLimit());
194
  }
195
196
}
197