mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Add Miscellaneous::calculateGreatestCommonDivisor() method
This commit is contained in:
@@ -1319,4 +1319,9 @@ class Miscellaneous
|
|||||||
|
|
||||||
return substr($string, 1);
|
return substr($string, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function calculateGreatestCommonDivisor(int $first, int $second): int
|
||||||
|
{
|
||||||
|
return (0 === $second) ? $first : static::calculateGreatestCommonDivisor($second, $first % $second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -735,6 +735,18 @@ class MiscellaneousTest extends BaseTestCase
|
|||||||
self::assertEquals($expected, Miscellaneous::removeMarginalCharacter($string, $last), $description);
|
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
|
* 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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user