-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpascal triangle with memoization.jl
207 lines (108 loc) · 3.31 KB
/
pascal triangle with memoization.jl
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
206
207
# coding: utf-8
# In[1]:
function pascal_triangle(n)
row=Any[]
#base case
if n==1
return Any[1]
elseif n==2
return Any[1,1]
else
#calculate the elements in each row
for i in 2:n-1
#rolling sum all the values within 2 windows from the previous row
#but we cannot include two boundary numbers 1 in this row
push!(row,pascal_triangle(n-1)[i-1]+pascal_triangle(n-1)[i])
end
#append 1 for both front and rear of the row
pushfirst!(row,1)
push!(row,1)
end
return row
end
# In[2]:
function styled_row(nested)
#the first loop is to concatenate all rows
for i in 1:length(nested)
temp=nested[i]
#this loop is to reshape the row
#insert '' between each element in the row
for j in 2:(2*i-1)
#if index k is an even number,insert ''
if j%2==0
insert!(temp,j,"")
end
end
#need to add '' to both sides of rows
#length(nested)-i=((length(nested)+length(nested)-1)-(i+i-1))/2
#we set the n th row plus n-1 space as the total elements in a row
#we minus the reshaped row (i+i-1)
#we get the space for both sides
#finally we divide it by 2 and add to both sides of the row
#we append the styled row into rows
nested[i]=cat(fill("",length(nested)-i),temp,fill("",length(nested)-i),dims=1)
end
return nested
end
# In[3]:
#print out each row
function printit(n)
nested=Any[]
for i in 1:n
push!(nested,pascal_triangle(i))
end
for i in styled_row(nested)
println(i)
end
end
# In[4]:
#using memoization
# In[5]:
function mmz(n)
row=Any[]
if !(n in keys(memoization))
#rolling sum all the values within 2 windows from the previous row
#but we cannot include two boundary numbers 1 in this row
for i in 2:n-1
push!(row,mmz(n-1)[i-1]+mmz(n-1)[i])
end
#append 1 for both front and rear of the row
pushfirst!(row,1)
push!(row,1)
global memoization[n]=row
end
return memoization[n]
end
# In[6]:
function printit_mmz(n)
global memoization=Dict(1=>Any[1],2=>Any[1,1])
nested=Any[]
for i in 1:n
push!(nested,mmz(i))
end
for i in styled_row(nested)
println(i)
end
end
# In[7]:
#0.021448 seconds (1.95 k allocations: 72.875 KiB)
@time begin
printit(5)
end
# In[8]:
#0.355459 seconds (344.14 k allocations: 17.351 MiB)
@time begin
printit_mmz(5)
end
# In[9]:
#3.822380 seconds (36.60 M allocations: 2.678 GiB, 8.57% gc time)
@time begin
printit(10)
end
# In[10]:
#0.232830 seconds (7.27 k allocations: 254.938 KiB)
@time begin
printit_mmz(10)
end
#at the first glance,memoization isnt faster
#as the number grows larger,memoization reveals its true nature