forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalan_number.js
More file actions
52 lines (47 loc) · 762 Bytes
/
catalan_number.js
File metadata and controls
52 lines (47 loc) · 762 Bytes
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
/*
Catalan numbers is a number sequence, which is found useful in a number of
combinatorial problems, often involving recursively-defined objects.
*/
var n = prompt("Enter the number:");
var c = [];
function catalan(n) {
if (n == 0) return 1;
if (!c[n]) {
var s = 0;
for (var i = 0; i < n; i++)
s += catalan(i) * catalan(n - i - 1);
c[n] = s;
}
return c[n];
}
document.write("Nth Catalan Number are:")
document.write("<br>");
for (var i = 0; i <= n; i++) {
document.write(catalan(i));
document.write("<br>");
}
/*
Input:
Enter the number:
15
Output:
Nth Catalan Number are:
1
1
2
5
14
42
132
429
1430
4862
16796
58786
208012
742900
2674440
9694845
Time Complexity: O(2^n)
Space Complexity: O(1)
*/