Advertisement
math2do

nth_catalan

May 2nd, 2020
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. /**
  2.  *    author:  Mathura Tudu
  3.  *    created: 25.04.2020 20:52:53 IST
  4. **/
  5.  
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. using ull = unsigned long long int;
  11.  
  12. ull binomial(int n, int k) {
  13.   ull res = 1;
  14.   if (k > n - k) {
  15.     k = n - k;
  16.   }
  17.   for (int i = 0; i < k; i++) {
  18.     res *= (n - i);
  19.     res /= (i + 1);
  20.   }
  21.   return res;
  22. }
  23.  
  24. ull catalan(int n) {
  25.   ull c = binomial(2*n, n);
  26.   // return 2nCn / (n + 1)
  27.   return c / (n + 1);
  28. }
  29. int main()  {
  30.   //catalan[n - 2] gives the answer for n-sided polygon
  31.   for (int n = 3; n <= 10; n++) {
  32.     cout << catalan(n - 2) << endl;
  33.   }
  34.   return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement