Catalan Numbers
Generate first n Catalan numbers.
AdvancedPhase 6 - Category 4: Mathematical CombinationsExample 7 of 10
catalan-numbers.py
1# Helper function2def catalan(n):3 if n == 0:4 return 15 result = 06 for i in range(n):7 result += catalan(i) * catalan(n - 1 - i)8 return result910# Take n11n = int(input("Enter n: "))1213# Print Catalan numbers14print(f"First {n} Catalan numbers:")15for i in range(n):16 print(catalan(i), end=" ")17print()
Output
Enter n: 10 First 10 Catalan numbers: 1 1 2 5 14 42 132 429 1430 4862
What's going on
Catalan numbers using recursive formula.
Key Concepts:
C(0) = 1
C(n) = sum of C(i) * C(n-1-i)
Recursive calculation
🔗Related Content
- 📖
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- 📝
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- 💻
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum