Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 6, 2016 17:12
Show Gist options
  • Save jianminchen/631086210db7c41fcdda677c3aaf726c to your computer and use it in GitHub Desktop.
Save jianminchen/631086210db7c41fcdda677c3aaf726c to your computer and use it in GitHub Desktop.
Great GCD - great common denominator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace facebookCodelab_greatGCD
{
class Program
{
static void Main(string[] args)
{
}
}
/*
* July 6, 2016
* Given 2 non negative integers m and n, find gcd(m, n)
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n.
Both m and n fit in a 32 bit signed integer.
Example
m : 6
n : 9
GCD(m, n) : 3
NOTE : DO NOT USE LIBRARY FUNCTIONS
*
*
*/
public class Solution
{
public int gcd(int a, int b)
{
if (a == b)
return a;
if (a < b)
{
int tmp = a;
a = b;
b = tmp;
}
if (b == 0) // bug001 - forget to check if(b==0) first writing, not reaching facebook coding requirement!
return a;
if (a % b == 0)
return b;
else
{
return gcd(a - b, b);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment