Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active April 9, 2021 15:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpscholar/3277578 to your computer and use it in GitHub Desktop.
Save wpscholar/3277578 to your computer and use it in GitHub Desktop.
Euclidean algorithm - Find the greatest common divisor of two integers
<?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