In [ ]:
def fib(n):
    if n == 0: return 1
    if n == 1: return 1
    return fib(n-1) + fib(n-2)
fib(33)
In [ ]:
def fib_memo(n):
    # A[i] is going to hold fib(i)
    A = [None] * (n+1)
    A[0] = 1
    A[1] = 1
    for i in range(2,n+1):
        A[i] = A[i-1] + A[i-2]
    return A[n]
fib_memo(3)
In [ ]:
def fib_fancy(n):
    prev1 = 1
    prev2 = 1
    for i in range(2,n+1):
        curr = prev1 + prev2
        # shift over
        prev2 = prev1
        prev1 = curr
    return curr
fib_fancy(8)
In [2]:
# Implicit Memoization
class Memoize:
    def __init__(self, function):
        self.function = function
        self.storage = {}
    def __call__(self, n):
        if n in self.storage:
            print("Using precomputed", n)            
            return self.storage[n]
        else:
            print("Computing the first time", n)
            ans = self.function(n)
            self.storage[n] = ans
            return ans
In [3]:
@Memoize
def fib(n):
    if n == 0: return 1
    if n == 1: return 1
    return fib(n-1) + fib(n-2)
fib(300)
Computing the first time 300
Computing the first time 299
Computing the first time 298
Computing the first time 297
Computing the first time 296
Computing the first time 295
Computing the first time 294
Computing the first time 293
Computing the first time 292
Computing the first time 291
Computing the first time 290
Computing the first time 289
Computing the first time 288
Computing the first time 287
Computing the first time 286
Computing the first time 285
Computing the first time 284
Computing the first time 283
Computing the first time 282
Computing the first time 281
Computing the first time 280
Computing the first time 279
Computing the first time 278
Computing the first time 277
Computing the first time 276
Computing the first time 275
Computing the first time 274
Computing the first time 273
Computing the first time 272
Computing the first time 271
Computing the first time 270
Computing the first time 269
Computing the first time 268
Computing the first time 267
Computing the first time 266
Computing the first time 265
Computing the first time 264
Computing the first time 263
Computing the first time 262
Computing the first time 261
Computing the first time 260
Computing the first time 259
Computing the first time 258
Computing the first time 257
Computing the first time 256
Computing the first time 255
Computing the first time 254
Computing the first time 253
Computing the first time 252
Computing the first time 251
Computing the first time 250
Computing the first time 249
Computing the first time 248
Computing the first time 247
Computing the first time 246
Computing the first time 245
Computing the first time 244
Computing the first time 243
Computing the first time 242
Computing the first time 241
Computing the first time 240
Computing the first time 239
Computing the first time 238
Computing the first time 237
Computing the first time 236
Computing the first time 235
Computing the first time 234
Computing the first time 233
Computing the first time 232
Computing the first time 231
Computing the first time 230
Computing the first time 229
Computing the first time 228
Computing the first time 227
Computing the first time 226
Computing the first time 225
Computing the first time 224
Computing the first time 223
Computing the first time 222
Computing the first time 221
Computing the first time 220
Computing the first time 219
Computing the first time 218
Computing the first time 217
Computing the first time 216
Computing the first time 215
Computing the first time 214
Computing the first time 213
Computing the first time 212
Computing the first time 211
Computing the first time 210
Computing the first time 209
Computing the first time 208
Computing the first time 207
Computing the first time 206
Computing the first time 205
Computing the first time 204
Computing the first time 203
Computing the first time 202
Computing the first time 201
Computing the first time 200
Computing the first time 199
Computing the first time 198
Computing the first time 197
Computing the first time 196
Computing the first time 195
Computing the first time 194
Computing the first time 193
Computing the first time 192
Computing the first time 191
Computing the first time 190
Computing the first time 189
Computing the first time 188
Computing the first time 187
Computing the first time 186
Computing the first time 185
Computing the first time 184
Computing the first time 183
Computing the first time 182
Computing the first time 181
Computing the first time 180
Computing the first time 179
Computing the first time 178
Computing the first time 177
Computing the first time 176
Computing the first time 175
Computing the first time 174
Computing the first time 173
Computing the first time 172
Computing the first time 171
Computing the first time 170
Computing the first time 169
Computing the first time 168
Computing the first time 167
Computing the first time 166
Computing the first time 165
Computing the first time 164
Computing the first time 163
Computing the first time 162
Computing the first time 161
Computing the first time 160
Computing the first time 159
Computing the first time 158
Computing the first time 157
Computing the first time 156
Computing the first time 155
Computing the first time 154
Computing the first time 153
Computing the first time 152
Computing the first time 151
Computing the first time 150
Computing the first time 149
Computing the first time 148
Computing the first time 147
Computing the first time 146
Computing the first time 145
Computing the first time 144
Computing the first time 143
Computing the first time 142
Computing the first time 141
Computing the first time 140
Computing the first time 139
Computing the first time 138
Computing the first time 137
Computing the first time 136
Computing the first time 135
Computing the first time 134
Computing the first time 133
Computing the first time 132
Computing the first time 131
Computing the first time 130
Computing the first time 129
Computing the first time 128
Computing the first time 127
Computing the first time 126
Computing the first time 125
Computing the first time 124
Computing the first time 123
Computing the first time 122
Computing the first time 121
Computing the first time 120
Computing the first time 119
Computing the first time 118
Computing the first time 117
Computing the first time 116
Computing the first time 115
Computing the first time 114
Computing the first time 113
Computing the first time 112
Computing the first time 111
Computing the first time 110
Computing the first time 109
Computing the first time 108
Computing the first time 107
Computing the first time 106
Computing the first time 105
Computing the first time 104
Computing the first time 103
Computing the first time 102
Computing the first time 101
Computing the first time 100
Computing the first time 99
Computing the first time 98
Computing the first time 97
Computing the first time 96
Computing the first time 95
Computing the first time 94
Computing the first time 93
Computing the first time 92
Computing the first time 91
Computing the first time 90
Computing the first time 89
Computing the first time 88
Computing the first time 87
Computing the first time 86
Computing the first time 85
Computing the first time 84
Computing the first time 83
Computing the first time 82
Computing the first time 81
Computing the first time 80
Computing the first time 79
Computing the first time 78
Computing the first time 77
Computing the first time 76
Computing the first time 75
Computing the first time 74
Computing the first time 73
Computing the first time 72
Computing the first time 71
Computing the first time 70
Computing the first time 69
Computing the first time 68
Computing the first time 67
Computing the first time 66
Computing the first time 65
Computing the first time 64
Computing the first time 63
Computing the first time 62
Computing the first time 61
Computing the first time 60
Computing the first time 59
Computing the first time 58
Computing the first time 57
Computing the first time 56
Computing the first time 55
Computing the first time 54
Computing the first time 53
Computing the first time 52
Computing the first time 51
Computing the first time 50
Computing the first time 49
Computing the first time 48
Computing the first time 47
Computing the first time 46
Computing the first time 45
Computing the first time 44
Computing the first time 43
Computing the first time 42
Computing the first time 41
Computing the first time 40
Computing the first time 39
Computing the first time 38
Computing the first time 37
Computing the first time 36
Computing the first time 35
Computing the first time 34
Computing the first time 33
Computing the first time 32
Computing the first time 31
Computing the first time 30
Computing the first time 29
Computing the first time 28
Computing the first time 27
Computing the first time 26
Computing the first time 25
Computing the first time 24
Computing the first time 23
Computing the first time 22
Computing the first time 21
Computing the first time 20
Computing the first time 19
Computing the first time 18
Computing the first time 17
Computing the first time 16
Computing the first time 15
Computing the first time 14
Computing the first time 13
Computing the first time 12
Computing the first time 11
Computing the first time 10
Computing the first time 9
Computing the first time 8
Computing the first time 7
Computing the first time 6
Computing the first time 5
Computing the first time 4
Computing the first time 3
Computing the first time 2
Computing the first time 1
Computing the first time 0
Using precomputed 1
Using precomputed 2
Using precomputed 3
Using precomputed 4
Using precomputed 5
Using precomputed 6
Using precomputed 7
Using precomputed 8
Using precomputed 9
Using precomputed 10
Using precomputed 11
Using precomputed 12
Using precomputed 13
Using precomputed 14
Using precomputed 15
Using precomputed 16
Using precomputed 17
Using precomputed 18
Using precomputed 19
Using precomputed 20
Using precomputed 21
Using precomputed 22
Using precomputed 23
Using precomputed 24
Using precomputed 25
Using precomputed 26
Using precomputed 27
Using precomputed 28
Using precomputed 29
Using precomputed 30
Using precomputed 31
Using precomputed 32
Using precomputed 33
Using precomputed 34
Using precomputed 35
Using precomputed 36
Using precomputed 37
Using precomputed 38
Using precomputed 39
Using precomputed 40
Using precomputed 41
Using precomputed 42
Using precomputed 43
Using precomputed 44
Using precomputed 45
Using precomputed 46
Using precomputed 47
Using precomputed 48
Using precomputed 49
Using precomputed 50
Using precomputed 51
Using precomputed 52
Using precomputed 53
Using precomputed 54
Using precomputed 55
Using precomputed 56
Using precomputed 57
Using precomputed 58
Using precomputed 59
Using precomputed 60
Using precomputed 61
Using precomputed 62
Using precomputed 63
Using precomputed 64
Using precomputed 65
Using precomputed 66
Using precomputed 67
Using precomputed 68
Using precomputed 69
Using precomputed 70
Using precomputed 71
Using precomputed 72
Using precomputed 73
Using precomputed 74
Using precomputed 75
Using precomputed 76
Using precomputed 77
Using precomputed 78
Using precomputed 79
Using precomputed 80
Using precomputed 81
Using precomputed 82
Using precomputed 83
Using precomputed 84
Using precomputed 85
Using precomputed 86
Using precomputed 87
Using precomputed 88
Using precomputed 89
Using precomputed 90
Using precomputed 91
Using precomputed 92
Using precomputed 93
Using precomputed 94
Using precomputed 95
Using precomputed 96
Using precomputed 97
Using precomputed 98
Using precomputed 99
Using precomputed 100
Using precomputed 101
Using precomputed 102
Using precomputed 103
Using precomputed 104
Using precomputed 105
Using precomputed 106
Using precomputed 107
Using precomputed 108
Using precomputed 109
Using precomputed 110
Using precomputed 111
Using precomputed 112
Using precomputed 113
Using precomputed 114
Using precomputed 115
Using precomputed 116
Using precomputed 117
Using precomputed 118
Using precomputed 119
Using precomputed 120
Using precomputed 121
Using precomputed 122
Using precomputed 123
Using precomputed 124
Using precomputed 125
Using precomputed 126
Using precomputed 127
Using precomputed 128
Using precomputed 129
Using precomputed 130
Using precomputed 131
Using precomputed 132
Using precomputed 133
Using precomputed 134
Using precomputed 135
Using precomputed 136
Using precomputed 137
Using precomputed 138
Using precomputed 139
Using precomputed 140
Using precomputed 141
Using precomputed 142
Using precomputed 143
Using precomputed 144
Using precomputed 145
Using precomputed 146
Using precomputed 147
Using precomputed 148
Using precomputed 149
Using precomputed 150
Using precomputed 151
Using precomputed 152
Using precomputed 153
Using precomputed 154
Using precomputed 155
Using precomputed 156
Using precomputed 157
Using precomputed 158
Using precomputed 159
Using precomputed 160
Using precomputed 161
Using precomputed 162
Using precomputed 163
Using precomputed 164
Using precomputed 165
Using precomputed 166
Using precomputed 167
Using precomputed 168
Using precomputed 169
Using precomputed 170
Using precomputed 171
Using precomputed 172
Using precomputed 173
Using precomputed 174
Using precomputed 175
Using precomputed 176
Using precomputed 177
Using precomputed 178
Using precomputed 179
Using precomputed 180
Using precomputed 181
Using precomputed 182
Using precomputed 183
Using precomputed 184
Using precomputed 185
Using precomputed 186
Using precomputed 187
Using precomputed 188
Using precomputed 189
Using precomputed 190
Using precomputed 191
Using precomputed 192
Using precomputed 193
Using precomputed 194
Using precomputed 195
Using precomputed 196
Using precomputed 197
Using precomputed 198
Using precomputed 199
Using precomputed 200
Using precomputed 201
Using precomputed 202
Using precomputed 203
Using precomputed 204
Using precomputed 205
Using precomputed 206
Using precomputed 207
Using precomputed 208
Using precomputed 209
Using precomputed 210
Using precomputed 211
Using precomputed 212
Using precomputed 213
Using precomputed 214
Using precomputed 215
Using precomputed 216
Using precomputed 217
Using precomputed 218
Using precomputed 219
Using precomputed 220
Using precomputed 221
Using precomputed 222
Using precomputed 223
Using precomputed 224
Using precomputed 225
Using precomputed 226
Using precomputed 227
Using precomputed 228
Using precomputed 229
Using precomputed 230
Using precomputed 231
Using precomputed 232
Using precomputed 233
Using precomputed 234
Using precomputed 235
Using precomputed 236
Using precomputed 237
Using precomputed 238
Using precomputed 239
Using precomputed 240
Using precomputed 241
Using precomputed 242
Using precomputed 243
Using precomputed 244
Using precomputed 245
Using precomputed 246
Using precomputed 247
Using precomputed 248
Using precomputed 249
Using precomputed 250
Using precomputed 251
Using precomputed 252
Using precomputed 253
Using precomputed 254
Using precomputed 255
Using precomputed 256
Using precomputed 257
Using precomputed 258
Using precomputed 259
Using precomputed 260
Using precomputed 261
Using precomputed 262
Using precomputed 263
Using precomputed 264
Using precomputed 265
Using precomputed 266
Using precomputed 267
Using precomputed 268
Using precomputed 269
Using precomputed 270
Using precomputed 271
Using precomputed 272
Using precomputed 273
Using precomputed 274
Using precomputed 275
Using precomputed 276
Using precomputed 277
Using precomputed 278
Using precomputed 279
Using precomputed 280
Using precomputed 281
Using precomputed 282
Using precomputed 283
Using precomputed 284
Using precomputed 285
Using precomputed 286
Using precomputed 287
Using precomputed 288
Using precomputed 289
Using precomputed 290
Using precomputed 291
Using precomputed 292
Using precomputed 293
Using precomputed 294
Using precomputed 295
Using precomputed 296
Using precomputed 297
Using precomputed 298
Out[3]:
359579325206583560961765665172189099052367214309267232255589801