1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-24 09:41:55 +01:00
CodeWars/5kyu/common_denominators/solution.cs
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

32 lines
842 B
C#

using System;
public class Fracts {
public static string convertFrac(long[,] lst)
{
if (lst.GetLength(0) < 1) return "";
long denominator = lst[0, 1];
string result = "";
for (int i = 0; i < lst.GetLength(0); i++) denominator = LeastCommonMultiple(denominator, lst[i, 1]);
for (int i = 0; i < lst.GetLength(0); i++)
{
long factor = denominator / lst[i, 1];
result += String.Format("({0},{1})", lst[i, 0] * factor, lst[i, 1] * factor);
}
return result;
}
public static long GreatestCommonDivisor(long a, long b)
{
if (b == 0) return a;
else return GreatestCommonDivisor(b, a % b);
}
public static long LeastCommonMultiple(long a, long b)
{
return a * b / GreatestCommonDivisor(a, b);
}
}