Problem:
Greatest Common Divisor

Write a program which finds the greatest common divisor of two natural numbers a and b

Input

a and b are given in a line sparated by a single space.

Output

Output the greatest common divisor of a and b.

Constrants

1 ≤ a, b ≤ 109

Hint

You can use the following observation:

For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y)

Sample Input 1

54 20

Sample Output 1

2

Sample Input 2

147 105

Sample Output 2

21

Solution in C# (judge verdict: Accepted)

using System;

namespace tst
{
    class Program
    {
        static void Main(string[] args)
        {   //「x y」で入力されたものをx,yに振り分ける
            string str = Console.ReadLine();
            string[] data = str.Split();
            int d1 = int.Parse(data[0]);
            int d2 = int.Parse(data[1]);
            //x>=yならxとyの最大公約数はxとx%yの大歳公約数に等しい
            //ex)x=15,y=10の時、x%y=5
            int x = 0;
            int y = 0;
            int GCD = 0;
            if (d1 >= d2)
            {
                x = d1;
                y = d2;

            }else if(d1 < d2){
                x = d2;
                y = d1;
            }
            if (x % y == 0)
            {
                GCD = y;
            }
            else
            {
                while(true)
                {
                    GCD = x % y;
                    if (y % GCD == 0)
                    {
                        break;
                    }
                    else
                    {
                        x = y;
                        y = GCD;
                    }
                }
            }
            Console.WriteLine(GCD);
        }
    }
}