Implementation of Pascal's Triangle in C#

suggest change
public static class PascalsTriangle
{
    static long Gcd(long a, long b)
    {
        while (b != 0)
        {
            long remainder = a % b;
            a = b;
            b = remainder;
        }
        return a;
    }

    public static void Print(int rows)
    {
        if (rows < 0)
            throw new System.ArgumentOutOfRangeException(nameof(rows));
        for (int row = 0; row < rows; row++)
        {
            long value = 1;
            for (int column = 0; column <= row; column++)
            {
                if (column > 0) System.Console.Write(' ');
                System.Console.Write(value);
                long numerator = row - column;
                long denominator = column + 1;
                long divisor = Gcd(numerator, denominator);
                numerator /= divisor;
                denominator /= divisor;
                divisor = Gcd(value, denominator);
                value /= divisor;
                denominator /= divisor;
                value = checked(value * numerator / denominator);
            }
            System.Console.WriteLine();
        }
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents