GCD Function

GCD Function

Easy PHP PHP Functions 29 views
Explanation Complexity

Problem Statement

Write a gcd(a,b) function and print gcd for the given inputs.

Input Format

One line: a b.

Output Format

One integer gcd.

Example

18 24
6

Constraints

0

Input / Output Format

Input Format
One line: a b.
Output Format
One integer gcd.
Constraints
0

Examples

Input:
18 24
Output:
6

Example Solution (Public)

PHP
<?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);
?>

Official Solution Code

<?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);
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.