Catalan numbers: Difference between revisions
Content deleted Content added
m →{{header|Fantom}}: and yet one more! |
No edit summary |
||
Line 7: | Line 7: | ||
Implement at least one of these algorithms and print out the first 15 Catalan numbers with each. [[Memoization]] is not required, but may be worth the effort when using the second method above. |
Implement at least one of these algorithms and print out the first 15 Catalan numbers with each. [[Memoization]] is not required, but may be worth the effort when using the second method above. |
||
;Cf.: |
|||
* [[Pascal's triangle]] |
|||
* [http://milan.milanovic.org/math/english/fibo/fibo4.html Catalan Numbers and the Pascal Triangle] |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 840: | Line 836: | ||
C(14) = 2674440 |
C(14) = 2674440 |
||
C(15) = 9694845 |
C(15) = 9694845 |
||
</pre> |
|||
===An Alternative Approach=== |
|||
<lang cpp>// Generate Catalan Numbers |
|||
// |
|||
// Nigel Galloway: June 9th., 2012 |
|||
// |
|||
#include <iostream> |
|||
int main() { |
|||
const int N = 15; |
|||
int t[N+2] = {0,1}; |
|||
for(int i = 1; i<=N; i++){ |
|||
for(int j = i; j>1; j--) t[j] = t[j] + t[j-1]; |
|||
t[i+1] = t[i]; |
|||
for(int j = i+1; j>1; j--) t[j] = t[j] + t[j-1]; |
|||
std::cout << t[i+1] - t[i] << " "; |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
{{out|Produces}} |
|||
<pre> |
|||
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 |
|||
</pre> |
</pre> |
||