From 9903c82496af84601d43e3630b400f17b66521d8 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Mon, 9 Sep 2019 09:41:07 +0200 Subject: [PATCH] Add Miscellaneous::calculateGreatestCommonDivisor() method --- src/Utilities/Miscellaneous.php | 5 +++ tests/Utilities/MiscellaneousTest.php | 57 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/Utilities/Miscellaneous.php b/src/Utilities/Miscellaneous.php index 4ba0e37..c33290d 100644 --- a/src/Utilities/Miscellaneous.php +++ b/src/Utilities/Miscellaneous.php @@ -1319,4 +1319,9 @@ class Miscellaneous return substr($string, 1); } + + public static function calculateGreatestCommonDivisor(int $first, int $second): int + { + return (0 === $second) ? $first : static::calculateGreatestCommonDivisor($second, $first % $second); + } } diff --git a/tests/Utilities/MiscellaneousTest.php b/tests/Utilities/MiscellaneousTest.php index 93af639..25a714f 100644 --- a/tests/Utilities/MiscellaneousTest.php +++ b/tests/Utilities/MiscellaneousTest.php @@ -735,6 +735,18 @@ class MiscellaneousTest extends BaseTestCase self::assertEquals($expected, Miscellaneous::removeMarginalCharacter($string, $last), $description); } + /** + * @param int $first + * @param int $second + * @param int $expected + * + * @dataProvider provideGreatestCommonDivisor + */ + public function testCalculateGreatestCommonDivisor(int $first, int $second, int $expected): void + { + static::assertSame($expected, Miscellaneous::calculateGreatestCommonDivisor($first, $second)); + } + /** * Provides string to convert characters to latin characters and not lower cased and not human-readable * @@ -1506,6 +1518,51 @@ class MiscellaneousTest extends BaseTestCase ]; } + public function provideGreatestCommonDivisor(): ?Generator + { + yield[ + 0, + 0, + 0, + ]; + + yield[ + 1, + 1, + 1, + ]; + + yield[ + 5, + 3, + 1, + ]; + + yield[ + 6, + 3, + 3, + ]; + + yield[ + 12, + 9, + 3, + ]; + + yield[ + 20, + 12, + 4, + ]; + + yield[ + 120, + 80, + 40, + ]; + } + /** * {@inheritdoc} */