Objective

Create a function to find the greatest common divisor of two integers.

Difficulty

Easy

Description

The greatest common divisor, or simply the gcd, of two non-negative integers ::a:: and ::b:: is the largest integer that divides both ::a:: and ::b::.

For instance, the greatest common divisor of ::10:: and ::20:: is ::10::. This is often written as ::\text{gcd}(10, 20) = 10::. All common divisors of ::10:: and ::20:: are ::1, 2, 5, 10::, where the largest value is ::10::. Hence, ::\text{gcd}(10, 20) = 10::.

Similarly, ::\text{gcd}(10, 3) = 1::. There is no common divisor of ::10:: and ::3:: except for ::1::, hence, it's the greatest common divisor. Such a pair of integers whose gcd is ::1:: are said to be relatively prime to each other.

As another example, ::\text{gcd}(10, 0) = 10::. On the same lines, ::\text{gcd}(0, 10) = 10:: as well. However, ::\text{gcd}(0, 0):: is undefined. Every integer can divide ::0::, and so likewise there is no limit to this expression.

In this exercise, you have to create a function gcd() that takes in two integers as arguments and returns back their gcd. If both the numbers are 0, the function should return INF.

You MUST implement a brute force algorithm using for that goes over every integer to see if it's a common divisor or not.

Here's an example of the usage of the function:

<?php

echo 'gcd(10, 20): ', gcd(10, 20), "\n";
echo 'gcd(20, 20): ', gcd(20, 20), "\n";
echo 'gcd(10, 0): ', gcd(10, 0), "\n";
echo 'gcd(0, 10): ', gcd(0, 10), "\n";
echo 'gcd(0, 0): ', gcd(0, 0), "\n";
gcd(10, 20): 10 gcd(20, 20): 20 gcd(10, 0): 10 gcd(0, 10): 10 gcd(0, 0): INF