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