Matthew Ellen .co .uk

Useful code snippets

lcm

int lcm(IEnumerable<int> numbers)
{
return numbers.Aggregate((a, b) => (a / gcd(a, b)) * b);
}
int gcd(int a, int b)
{
if((b==0) && (a!=0))
{
return a;
}

if (a % b == 0)
{
return b;
}

return (gcd(b, a % b));
}

Back to the top

Back to code snippets