feat: Add Catalan Numbers algorithm implementation to mathematics module #191
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces an implementation of Catalan Numbers calculation algorithms to the mathematics module. Catalan numbers are widely used in combinatorics and appear in various counting problems in computer science and mathematics.
What’s Added
• Dynamic Programming Approach (catalan_dp): Efficient O(n) time complexity implementation.
• Direct Formula Approach (catalan_formula): Alternative calculation using the combinatorial formula.
• Utility Functions:
• first_n_catalan(): Generates the first n Catalan numbers.
• catalan_applications(): Demonstrates real-world applications of Catalan numbers.
Key Features
• Performance: O(n) time and space complexity.
• Educational: Includes mathematical explanations and documentation.
• Practical: Multiple approaches provided for flexibility.
• Examples: Sample usage demonstrating various applications.
Applications Covered
• Counting valid parentheses combinations
• Enumerating full binary trees
• Triangulating convex polygons
• Counting monotonic lattice paths
• Arranging non-attacking rooks
File Added
• mathematics/catalan_numbers.r
Testing
The implementation includes several example use cases and has been validated against known Catalan number sequences.
⸻
Type: Feature
Module: Mathematics
Breaking Changes: None