-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathEuler Zig-Zag Numbers.cpp
55 lines (43 loc) · 1.11 KB
/
Euler Zig-Zag Numbers.cpp
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
// CPP program to find zigzag sequence
#include <bits/stdc++.h>
using namespace std;
// Function to print first n zigzag numbers
void ZigZag(int n)
{
// To store factorial and n'th zig zag number
long long fact[n + 1], zig[n + 1] = { 0 };
// Initialize factorial upto n
fact[0] = 1;
for (int i = 1; i <= n; i++)
fact[i] = fact[i - 1] * i;
// Set first two zig zag numbers
zig[0] = 1;
zig[1] = 1;
// Print first two zig zag number
cout << zig[0] << " " << zig[1] << " ";
// Print the rest zig zag numbers
for (int i = 2; i < n; i++)
{
long long sum = 0;
for (int k = 0; k <= i - 1; k++)
{
// Binomial(n, k)*a(k)*a(n-k)
sum += (fact[i - 1]/(fact[i - 1 - k]*fact[k]))
*zig[k] * zig[i - 1 - k];
}
// Store the value
zig[i] = sum / 2;
// Print the number
cout << sum / 2 << " ";
}
}
// Driver code
int main()
{
int n;
cin >> n;
// Function call
for ( int i =0; i <n ;i++)
ZigZag(n);
return 0;
}