Skip to content

Instantly share code, notes, and snippets.

@DigiTec
Created August 25, 2014 02:46
Show Gist options
  • Select an option

  • Save DigiTec/cb98ab1fcd1aee11e12e to your computer and use it in GitHub Desktop.

Select an option

Save DigiTec/cb98ab1fcd1aee11e12e to your computer and use it in GitHub Desktop.
Computation for pascal's triangle values by calculating along the row so I don't lose it in the future. This is susceptible to round-off errors because we multiply before a divide when the triangle gets very large.
// Assumes row/col are 1 indexed
int PascalsMath(int row, int col)
{
// Enable mirroring about the central pivot of the row
col = min(col, row - col + 1);
int value = 1;
int numerator = row - 1;
int denominator = 1;
while (--col > 0)
{
value = (value * numerator--) / denominator++;
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment