I have built recursive function to compute Pascal's triangle values.
Is there a way to optimize it?
Short reminder about Pascal's triangle: C(n, k) = C(n-1, k-1) + C(n-1, k) My code is:
int Pascal(int n, int k) {
if (k == 0) return 1;
if (n == 0) return 0;
return Pascal(n - 1, k - 1) + Pascal(n - 1, k);
}
The inefficiency I see is that it stores some values twice. Example: C(6,2) = C(5,1) + C(5,2) C(6,2) = C(4,0) + C(4,1) + C(4,1) + C(4,2) it will call C(4,1) twice
Any idea how to optimize this function?
Thanks