Created
August 25, 2014 02:46
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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