Last active
April 9, 2021 15:43
-
-
Save wpscholar/3277578 to your computer and use it in GitHub Desktop.
Euclidean algorithm - Find the greatest common divisor of two integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Returns the greatest common divisor of two integers using the Euclidean algorithm. | |
* | |
* @param $a | |
* @param $b | |
* | |
* @return int | |
*/ | |
function get_greatest_common_divisor( $a, $b ) { | |
$large = $a > $b ? $a: $b; | |
$small = $a > $b ? $b: $a; | |
$remainder = $large % $small; | |
return 0 == $remainder ? $small : get_greatest_common_divisor( $small, $remainder ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment