Add Miscellaneous::calculateGreatestCommonDivisor() method

This commit is contained in:
Meritoo
2019-09-09 09:41:07 +02:00
parent 678cdfdf01
commit 9903c82496
2 changed files with 62 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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}
*/