Skip to content

DP program: Catalan numbers #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2019
Merged

Conversation

rafi007akhtar
Copy link
Contributor

@rafi007akhtar rafi007akhtar commented Nov 10, 2018

Program for Catalan numbers

Description

A classic DP problem that is often asked in coding contests and interviews is solving for the nth Catalan number. Catalan numbers have numerous applications, like possible number of expressions with correctly matched parentheses, possible number of BSTs with n keys, and so many more. [Applications of Catalan Numbers - GeeksforGeeks]

In my program

Using the technique of tabulation in dynamic programming, I have written a program that computes and prints the first n+1 Catalan numbers, where n is given by the user.

Although Catalan numbers have a factorial-based definition, the definition I used was a recursive one. This definition is written in the program as comments (shown below), and also available in its Uncyclopedia Page.

* A Catalan number satifies the following two properties:
 * C(0) = C(1) = 1; C(n) = sum(C(i).C(n-i-1)), from i = 0 to n-1

Why should this program be included in this repository

  • It is a classic DP problem that was thus far missing from this repository.
  • Satisfies all the requirements in the README.
    • Directory structure followed.
    • This PR is fully described.
    • bits/stdc++.h is not used; instead I have used the classic iostream.
    • I have commented the code as much as I could.
    • Didn't need struct or class, so didn't use.
    • Supplied a sample test case in the comments (as I didn't want to put any in the main function).
  • Solves the problem correctly using a classic DP technique,

Ending notes

If there is any mistake in my program, please notify in this PR conversation, and I shall try to improve them.

Looking forward to seeing this program in the repository!

@ashwek
Copy link
Member

ashwek commented Feb 10, 2019

@rafi007akhtar Program works fine for inputs less than 20.
For n = 20, Catalan number = 6564120420 (overflows int's capacity). You can use unsigned int to extend the range.
Accepting PR. 👍

@ashwek ashwek merged commit f3ba908 into TheAlgorithms:master Feb 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants