-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path12_pascal_triangle_1.cpp
103 lines (83 loc) · 2.71 KB
/
12_pascal_triangle_1.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
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
/*
Pascal Triangle 1
Given an integer N, print Pascal Triangle upto N rows.
Input Format: Single integer N.
Constraints: N <= 10
Output Format: Print pascal triangle.
Sample Input: 4
Sample Output: 1
1 1
1 2 1
1 3 3 1
*/
#include<iostream>
using namespace std;
int main() {
int total_rows;
cin >> total_rows;
int nos = total_rows; // number of spaces in first row
int nop = 1; // number of pattern in first row
for(int row=1; row<=total_rows; row++){
int cos; // counter of star
int cop; // counter of pattern
// print spaces
for(cos=1; cos<=nos; cos++){
cout << " ";
}
// print pattern - pascal number
int num = 1;
for(cop=1; cop<=nop; cop++){
if(cop==1 || cop==row){
cout << 1 << " ";
}else{
num=num*(row-cop+1)/(cop-1); // updating number
cout << num <<" ";
}
}
// for next iterations
nos--;
nop++;
cout << endl;
}
return 0;
}
/*
Pascal's triangle is the triangular array of the binomial coefficients.The number of entries in a given line is equal to the line number.Every entry in the line is the value of a binomial coefficient. The value of ith entry in the number line is C(line,i)(i.e apply mathematical combination formula ).
Where C(line,i)=line!/((line-i)!*i!).
Given pattern can be seen as first spaces then numbers and can be formed using 3 approches:
1.A simple method is to run two loops and calculate the value of binomial coefficient in inner loop. Complexity: O(N3).
2.O(N2) time and O(n2) space complexity.In this method store the previously generated values in 2-D array. Use these values to generate value in a line.
3.O(N2) time and O(1) space complexity. In this method calculate C(line,i) using C(line,i-1). It can be calculated in O(1) time as follows:
C(line,i-1)=line!/((line-i+1)!*(i-1)!)
C(line,i)=line!/((line-i)!*i!)
C(line,i)=C(line,i-1)*(line-i+1)/i.
Code:
#include<iostream>
using namespace std;
int main() {
int total_rows;
cin >> total_rows;
int row,col;
//work for each row
for(row=1;row<=total_rows;row++){
int num=1; //starting number
//work for spaces
for(int spaces=1; spaces<=(total_rows-row+1); spaces++){
cout << " ";
}
// print pattern - pascal number
for(col=1; col<=row; col++){
if(col==1)
cout << col << " ";
else
{
num=num*(row-col+1)/(col-1); // updating number
cout << num << " ";
}
}
// for next iteration
cout << endl;
}
return 0;
}
*/