From 2553639335f4f5885e61e6e5a7c9c0cdbda82735 Mon Sep 17 00:00:00 2001 From: Carlos Afonso Date: Sun, 22 Oct 2023 21:35:08 +0100 Subject: [PATCH 1/2] Add Greatest Common Divisor --- Maths/GreatestCommonDivisor.php | 16 ++++++++++++++++ tests/Maths/MathsTest.php | 11 +++++++++++ 2 files changed, 27 insertions(+) create mode 100644 Maths/GreatestCommonDivisor.php diff --git a/Maths/GreatestCommonDivisor.php b/Maths/GreatestCommonDivisor.php new file mode 100644 index 00000000..54758359 --- /dev/null +++ b/Maths/GreatestCommonDivisor.php @@ -0,0 +1,16 @@ + 6 + * @param int $a + * @param int $b + * @return int + */ +function gcd(int $a, int $b): int +{ + if ($b == 0) { + return $a; + } + return gcd($b, $a % $b); +} diff --git a/tests/Maths/MathsTest.php b/tests/Maths/MathsTest.php index 3d6f3008..e4222889 100644 --- a/tests/Maths/MathsTest.php +++ b/tests/Maths/MathsTest.php @@ -12,6 +12,7 @@ require_once __DIR__ . '/../../Maths/FastExponentiation.php'; require_once __DIR__ . '/../../Maths/Fibonacci.php'; require_once __DIR__ . '/../../Maths/Fibonacci2.php'; +require_once __DIR__ . '/../../Maths/GreatestCommonDivisor.php'; require_once __DIR__ . '/../../Maths/NeonNumber.php'; require_once __DIR__ . '/../../Maths/PerfectSquare.php'; require_once __DIR__ . '/../../Maths/Mean.php'; @@ -143,4 +144,14 @@ public function testMode() $this->assertEquals([1, 2, 3, 4, 5], mode(1, 2, 3, 4, 5)); $this->assertEquals([2, 3, 4], mode(2, 2, 3, 3, 4, 4)); } + + public function testGreatestCommonDivisor() + { + $this->assertEquals(8, gcd(24, 16)); + $this->assertEquals(5, gcd(10, 5)); + $this->assertEquals(25, gcd(100, 75)); + $this->assertEquals(6, gcd(12, 18)); + $this->assertEquals(5, gcd(10, 15)); + $this->assertEquals(3, gcd(9, 12)); + } } From 7bb741407983e022c78277069747eb8a3a05373d Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:35:49 +0000 Subject: [PATCH 2/2] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d6df201e..e4f956d3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -35,6 +35,7 @@ * [Fastexponentiation](./Maths/FastExponentiation.php) * [Fibonacci](./Maths/Fibonacci.php) * [Fibonacci2](./Maths/Fibonacci2.php) + * [Greatestcommondivisor](./Maths/GreatestCommonDivisor.php) * [Mean](./Maths/Mean.php) * [Median](./Maths/Median.php) * [Mode](./Maths/Mode.php)