Created
January 28, 2016 10:15
-
-
Save eshacker/66b0b3e2e108a18d3d18 to your computer and use it in GitHub Desktop.
Finding GCD recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Solution { | |
static void Main(String[] args) { | |
var numbers = Console.ReadLine().Split(' '); | |
var x = int.Parse(numbers[0]); | |
var y = int.Parse(numbers[1]); | |
Console.WriteLine(gcd(x, y)); | |
} | |
static int gcd(int x, int y){ | |
if(x % y == 0){ | |
return y; | |
} | |
else { | |
var min = Math.Min(x, y); | |
var max = Math.Max(x, y); | |
return gcd(max%min, min); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment