Here’s a fun little project I completed recently just because: a Lowest Common Denominator Calculator!

Of course, determining the LCD of 2 or more fractions is relatively trivial, but putting it into code and seeing it rendered as a tool was just somehow fun and satisfying to do.

Once I had created the C# code that would produce the answer, it seemed like a neat little UI thingy to throw up on a page and surround with ads ðŸ˜€

You can try it out here:

LCD – Lowest Common Denominator Calculator

Here’s the heart of the calculator in C# – a variable number of fractions get’s passed as input, and the lowest common denominator is calculated as output. The first thing that happens is the highest denominator of the input fractions is determined using a LINQ statement, following that – a potentially recursive function call does the rest:

private static long FindLowestCommonDenominator(params Fraction[] fractions) { if (fractions == null) { return 0; } var largestDenominator = fractions.OrderByDescending(x => x.D).FirstOrDefault()?.D; var others = fractions.Where(x => x.D != largestDenominator).Select(n=>n.D); return largestDenominator == null ? 0 : FindLowestCommonInteger((long)largestDenominator, 1, others.ToArray()); } private static long FindLowestCommonInteger(long largestDenominator, int factor, params long[] others) { var lcd = largestDenominator * factor; return others.Any(denominator => (lcd % denominator) != 0) ? FindLowestCommonInteger(largestDenominator, factor+1, others) : lcd; } |