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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    public void Proc()
    {
        string[] inpt = Console.ReadLine().Split(' ');
        int x = int.Parse(inpt[0]);
        int y = int.Parse(inpt[1]);

        List<int> soinsuX = this.GetYakusu(x);
        List<int> soinsuY = this.GetYakusu(y);

        int num = 1;
        for(int i=0; i<soinsuX.Count; i++)
        {
            for(int j=0; j<soinsuY.Count; j++)
            {
                if(soinsuX[i] == soinsuY[j])
                {
                    num *= soinsuX[i];
                    soinsuY.RemoveAt(j);
                    break;
                }
            }
        }
        Console.WriteLine(num);
    }

    private List<int> GetYakusu(int src)
    {
        int num = src;
        List<int> souinsu = new List<int>();
        while(true)
        {
            bool isMatch = false;
            for(int i=2; i< src; i++)
            {
                if(num % i == 0)
                {
                    souinsu.Add(i);
                    isMatch = true;
                    num = num / i;
                    break;
                }
            }
            if(!isMatch)
            {
                break;
            }
        }
        return souinsu;
    }


    private string ArrayJoin(int[] arr)
    {
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<arr.Length; i++)
        {
            if(sb.Length > 0)
            {
                sb.Append(" ");
            }
            sb.Append(arr[i]);
        }
        return sb.ToString();
    }

    static void Main(string[] args)

    {

        Program prg = new Program();
        prg.Proc();

    }
}