GCD Function
PHP
Easy
3 views
Problem Description
Write a gcd(a,b) function and print gcd for the given inputs.
Input Format
One line: a b.
Output Format
One integer gcd.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$a,$b]=array_map('intval',preg_split('/\\s+/', $inputText));
function gcd($x,$y){
$x=abs($x); $y=abs($y);
while($y!==0){ $t=$x%$y; $x=$y; $y=$t; }
return $x;
}
echo gcd($a,$b);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!