1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpermutations\Iterators; |
4
|
|
|
|
5
|
|
|
use drupol\phpermutations\Combinatorics; |
6
|
|
|
use drupol\phpermutations\IteratorInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class FiniteGroup. |
10
|
|
|
* |
11
|
|
|
* The finite group is an abelian finite cyclic group. |
12
|
|
|
*/ |
13
|
|
|
class FiniteGroup extends Combinatorics implements IteratorInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The group size. |
17
|
|
|
* |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $size; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The group. |
24
|
|
|
* |
25
|
|
|
* @var int[] |
26
|
|
|
*/ |
27
|
|
|
protected $group; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The key. |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $key; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Combinatorics constructor. |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct() |
40
|
|
|
{ |
41
|
2 |
|
parent::__construct([], null); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
2 |
|
public function current() |
48
|
|
|
{ |
49
|
2 |
|
return current($this->group); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
2 |
|
public function next() |
56
|
|
|
{ |
57
|
2 |
|
++$this->key; |
58
|
2 |
|
next($this->group); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
2 |
|
public function valid() |
65
|
|
|
{ |
66
|
2 |
|
return isset($this->group[$this->key()]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
2 |
|
public function rewind() |
73
|
|
|
{ |
74
|
2 |
|
$this->key = 0; |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Count elements of an object. |
79
|
|
|
* |
80
|
|
|
* @return int |
81
|
|
|
* The number of element |
82
|
|
|
*/ |
83
|
2 |
|
public function count() |
84
|
|
|
{ |
85
|
2 |
|
return count($this->group); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the group size. |
90
|
|
|
* |
91
|
|
|
* @param int $size |
92
|
|
|
* The size |
93
|
|
|
*/ |
94
|
2 |
|
public function setSize($size) |
95
|
|
|
{ |
96
|
2 |
|
$this->size = $size; |
97
|
2 |
|
$this->computeGroup(); |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the group size. |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
* The size |
105
|
|
|
*/ |
106
|
2 |
|
public function getSize() |
107
|
|
|
{ |
108
|
2 |
|
return (int) $this->size; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the order. |
113
|
|
|
* |
114
|
|
|
* @param int $generator |
115
|
|
|
* The generator |
116
|
|
|
* |
117
|
|
|
* @return int |
118
|
|
|
* The order |
119
|
|
|
*/ |
120
|
|
|
public function order($generator) |
121
|
|
|
{ |
122
|
|
|
$result = []; |
123
|
|
|
|
124
|
|
|
foreach (range(1, $this->getSize() - 1) as $number) { |
125
|
|
|
$value = ($generator ** $number) % $this->getSize(); |
126
|
|
|
$result[$value] = $value; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return count($result); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Clean out the group from unwanted values. |
134
|
|
|
*/ |
135
|
2 |
|
private function computeGroup() |
136
|
|
|
{ |
137
|
2 |
|
$this->group = []; |
138
|
|
|
|
139
|
2 |
|
foreach (range(1, $this->getSize() - 1) as $number) { |
140
|
2 |
|
if (1 === $this->gcd($number, $this->getSize() - 1)) { |
141
|
2 |
|
$this->group[] = $number; |
142
|
|
|
} |
143
|
|
|
} |
144
|
2 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the greater common divisor between two numbers. |
148
|
|
|
* |
149
|
|
|
* @param int $a |
150
|
|
|
* The first number |
151
|
|
|
* @param int $b |
152
|
|
|
* The second number |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
* The greater common divisor between $a and $b |
156
|
|
|
*/ |
157
|
2 |
|
private function gcd($a, $b) |
158
|
|
|
{ |
159
|
2 |
|
return $b ? $this->gcd($b, $a % $b) : $a; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|