-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy patheuclidean.php
More file actions
35 lines (28 loc) · 683 Bytes
/
euclidean.php
File metadata and controls
35 lines (28 loc) · 683 Bytes
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
<?php
declare(strict_types=1);
function euclid_sub(int $a, int $b): int
{
$a = abs($a);
$b = abs($b);
while ($a !== $b) {
if ($a > $b) {
$a = $a - $b;
} else {
$b = $b - $a;
}
}
return $a;
}
function euclid_mod(int $a, int $b): int
{
$a = abs($a);
$b = abs($b);
while ($b !== 0) {
list($b, $a) = [$a % $b, $b];
}
return $a;
}
printf('[#]'.PHP_EOL.'Modulus-based euclidean algorithm result:'.PHP_EOL.'%s', euclid_mod(64 * 67, 64 * 81));
echo PHP_EOL;
printf('[#]'.PHP_EOL.'Subtraction-based euclidean algorithm result:'.PHP_EOL.'%s', euclid_sub(128 * 12, 128 * 77));
echo PHP_EOL;