1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/// Generate Catalan numbers up to n terms
///
/// The Catalan numbers form a sequence of natural numbers that appear in numerous
/// combinatorial problems. Named after Eugène Charles Catalan (1814-1894), they count:
/// - Binary trees with n+1 leaves
/// - Ways to parenthesize expressions
/// - Paths that don't cross the diagonal in a grid
/// - Ways to triangulate a polygon
///
/// The sequence goes: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862...
///
/// # Formula
/// C(n) = (2n)! / ((n+1)! × n!)
/// Or recursively: C(0) = 1, C(n+1) = Σ C(i)×C(n-i) for i=0 to n
///
/// # Mathematical Properties
/// - Growth rate: C(n) ~ 4ⁿ / (n^(3/2) × √π)
/// - Faster than Fibonacci, slower than exponential
/// - Central binomial coefficients divided by n+1
/// - Appears in probability, graph theory, and computer science
///
/// # Musical Character
/// Catalan numbers have a beautiful moderate growth rate - not too fast (like 2^n),
/// not too slow (like n²). This makes them ideal for:
/// - Natural-feeling crescendos and accelerandos
/// - Phrase lengths that expand organically
/// - Harmonic complexity that grows but stays manageable
/// - Rhythmic subdivisions that increase gradually
///
/// # Arguments
/// * `n` - Number of Catalan terms to generate (typically 1-15 for musical use)
///
/// # Returns
/// Vector of the first n Catalan numbers: [1, 1, 2, 5, 14, 42, ...]
///
/// # Examples
/// ```
/// use tunes::sequences;
///
/// let catalan = sequences::catalan::generate(8);
/// assert_eq!(catalan, vec![1, 1, 2, 5, 14, 42, 132, 429]);
///
/// // Use for growing rhythmic density
/// # use tunes::prelude::*;
/// # let mut comp = Composition::new(Tempo::new(120.0));
/// // Normalize to note durations
/// let durations = sequences::normalize(&catalan[0..6], 0.0625, 0.5);
/// for (i, &duration) in durations.iter().enumerate() {
/// comp.track("catalan_rhythm")
/// .at(i as f32 * 0.5)
/// .note(&[440.0], duration);
/// }
///
/// // Use for expanding chord voicings
/// let chord_notes = vec![
/// vec![C4], // C(0) = 1 note
/// vec![C4, E4], // C(1) = 1, but use 2
/// vec![C4, E4, G4], // C(2) = 2, but use 3
/// ];
/// ```
///
/// # Musical Applications
/// - **Dynamic expansion**: Use for crescendos with natural feel
/// - **Rhythmic acceleration**: Subdivisions that multiply organically
/// - **Harmonic density**: Number of notes in successive chords
/// - **Phrase lengths**: Measures per section (1, 2, 5, 14 bars)
/// - **Voice leading**: Number of voice movements in counterpoint
/// - **Texture building**: Layer count increasing naturally
/// - **Filter sweeps**: Cutoff frequency changes with organic curve
///
/// # Note on Size
/// Catalan numbers grow quickly! C(15) = 9,694,845. For musical applications,
/// typically use the first 8-12 terms and normalize/scale appropriately.
// ========== PRESETS ==========
/// Short Catalan sequence (8 terms)
/// Classic Catalan sequence (10 terms)
/// Extended Catalan sequence (12 terms)