|
82 | 82 | "metadata": {}, |
83 | 83 | "outputs": [], |
84 | 84 | "source": [ |
85 | | - "sys.setrecursionlimit(1000000)" |
| 85 | + "sys.setrecursionlimit(1_000_000)" |
86 | 86 | ] |
87 | 87 | }, |
88 | 88 | { |
|
164 | 164 | "cell_type": "markdown", |
165 | 165 | "metadata": {}, |
166 | 166 | "source": [ |
167 | | - "### Factorial definition\n", |
| 167 | + "### Sum of First N positive integers\n", |
168 | 168 | "\n", |
169 | | - "```\n", |
170 | | - " 1! = 1 (base case)\n", |
171 | | - " n! = n.(n-1)! for n > 1 (general case)\n", |
172 | | - "```\n", |
| 169 | + "- use recursion to find the sum of first N positive integers\n", |
173 | 170 | "\n", |
174 | | - "- Exercise - Implement factorial recursive solution" |
| 171 | + "```\n", |
| 172 | + " first_sum(1) = 1 (base case)\n", |
| 173 | + " first_sum(n) = n + first_sum(n-1) for n > 1 (general case)\n", |
| 174 | + "```" |
175 | 175 | ] |
176 | 176 | }, |
177 | 177 | { |
178 | 178 | "cell_type": "code", |
179 | | - "execution_count": 7, |
| 179 | + "execution_count": 1, |
180 | 180 | "metadata": {}, |
181 | 181 | "outputs": [], |
182 | 182 | "source": [ |
183 | | - "def factorial(n):\n", |
| 183 | + "def first_sum(n):\n", |
184 | 184 | " if n == 1:\n", |
185 | 185 | " return 1\n", |
186 | 186 | " else:\n", |
187 | | - " return n*factorial(n-1)" |
| 187 | + " return n + first_sum(n-1)" |
188 | 188 | ] |
189 | 189 | }, |
190 | 190 | { |
191 | 191 | "cell_type": "code", |
192 | | - "execution_count": 8, |
193 | | - "metadata": {}, |
194 | | - "outputs": [ |
195 | | - { |
196 | | - "data": { |
197 | | - "text/plain": [ |
198 | | - "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000" |
199 | | - ] |
200 | | - }, |
201 | | - "execution_count": 8, |
202 | | - "metadata": {}, |
203 | | - "output_type": "execute_result" |
204 | | - } |
205 | | - ], |
206 | | - "source": [ |
207 | | - "factorial(100)" |
208 | | - ] |
209 | | - }, |
210 | | - { |
211 | | - "cell_type": "code", |
212 | | - "execution_count": 9, |
213 | | - "metadata": {}, |
214 | | - "outputs": [], |
215 | | - "source": [ |
216 | | - "from timeit import timeit" |
217 | | - ] |
218 | | - }, |
219 | | - { |
220 | | - "cell_type": "code", |
221 | | - "execution_count": 15, |
| 192 | + "execution_count": 2, |
222 | 193 | "metadata": {}, |
223 | 194 | "outputs": [ |
224 | 195 | { |
225 | 196 | "data": { |
226 | 197 | "text/plain": [ |
227 | | - "0.032029791036620736" |
| 198 | + "5050" |
228 | 199 | ] |
229 | 200 | }, |
230 | | - "execution_count": 15, |
| 201 | + "execution_count": 2, |
231 | 202 | "metadata": {}, |
232 | 203 | "output_type": "execute_result" |
233 | 204 | } |
234 | 205 | ], |
235 | 206 | "source": [ |
236 | | - "# run it \n", |
237 | | - "timeit('factorial(10_000)', globals=globals(), number=1)" |
| 207 | + "first_sum(100)" |
238 | 208 | ] |
239 | 209 | }, |
240 | 210 | { |
241 | 211 | "cell_type": "code", |
242 | | - "execution_count": 11, |
| 212 | + "execution_count": 3, |
243 | 213 | "metadata": {}, |
244 | 214 | "outputs": [], |
245 | 215 | "source": [ |
246 | 216 | "# use tail recursion optimization\n", |
247 | 217 | "# Python doesn't optimize tail recursion \n", |
248 | 218 | "# read the blog by Guido van Rossum - http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html\n", |
249 | | - "def factorial_tail(n, running_tot=1):\n", |
| 219 | + "def sum_tail(n, running_tot=1):\n", |
250 | 220 | " if n == 1:\n", |
251 | 221 | " return running_tot\n", |
252 | 222 | " else:\n", |
253 | | - " return factorial_tail(n-1, running_tot*n)" |
| 223 | + " return sum_tail(n-1, running_tot+n)" |
254 | 224 | ] |
255 | 225 | }, |
256 | 226 | { |
257 | 227 | "cell_type": "code", |
258 | | - "execution_count": 12, |
259 | | - "metadata": {}, |
260 | | - "outputs": [ |
261 | | - { |
262 | | - "data": { |
263 | | - "text/plain": [ |
264 | | - "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000" |
265 | | - ] |
266 | | - }, |
267 | | - "execution_count": 12, |
268 | | - "metadata": {}, |
269 | | - "output_type": "execute_result" |
270 | | - } |
271 | | - ], |
272 | | - "source": [ |
273 | | - "factorial_tail(100, 1)" |
274 | | - ] |
275 | | - }, |
276 | | - { |
277 | | - "cell_type": "code", |
278 | | - "execution_count": 14, |
| 228 | + "execution_count": 6, |
279 | 229 | "metadata": {}, |
280 | 230 | "outputs": [ |
281 | 231 | { |
282 | 232 | "data": { |
283 | 233 | "text/plain": [ |
284 | | - "0.07616704609245062" |
| 234 | + "5050" |
285 | 235 | ] |
286 | 236 | }, |
287 | | - "execution_count": 14, |
| 237 | + "execution_count": 6, |
288 | 238 | "metadata": {}, |
289 | 239 | "output_type": "execute_result" |
290 | 240 | } |
291 | 241 | ], |
292 | 242 | "source": [ |
293 | | - "# in fact it takes longer for Python as tail recurion is not optimized\n", |
294 | | - "# stack usage is O(n)\n", |
295 | | - "timeit('factorial_tail(10_000, 1)', globals=globals(), number=1)" |
| 243 | + "sum_tail(100, 1)" |
296 | 244 | ] |
297 | 245 | }, |
298 | 246 | { |
|
319 | 267 | }, |
320 | 268 | { |
321 | 269 | "cell_type": "code", |
322 | | - "execution_count": 16, |
| 270 | + "execution_count": null, |
323 | 271 | "metadata": {}, |
324 | 272 | "outputs": [], |
325 | 273 | "source": [ |
|
336 | 284 | }, |
337 | 285 | { |
338 | 286 | "cell_type": "code", |
339 | | - "execution_count": 17, |
| 287 | + "execution_count": null, |
340 | 288 | "metadata": {}, |
341 | | - "outputs": [ |
342 | | - { |
343 | | - "data": { |
344 | | - "text/plain": [ |
345 | | - "2178309" |
346 | | - ] |
347 | | - }, |
348 | | - "execution_count": 17, |
349 | | - "metadata": {}, |
350 | | - "output_type": "execute_result" |
351 | | - } |
352 | | - ], |
| 289 | + "outputs": [], |
353 | 290 | "source": [ |
354 | 291 | "fib(32)\n", |
355 | 292 | "#print(count)\n", |
|
369 | 306 | }, |
370 | 307 | { |
371 | 308 | "cell_type": "code", |
372 | | - "execution_count": 18, |
| 309 | + "execution_count": null, |
373 | 310 | "metadata": {}, |
374 | | - "outputs": [ |
375 | | - { |
376 | | - "data": { |
377 | | - "text/plain": [ |
378 | | - "0.5792814260348678" |
379 | | - ] |
380 | | - }, |
381 | | - "execution_count": 18, |
382 | | - "metadata": {}, |
383 | | - "output_type": "execute_result" |
384 | | - } |
385 | | - ], |
| 311 | + "outputs": [], |
386 | 312 | "source": [ |
387 | 313 | "timeit(\"fib(32)\", globals=globals(), number=1)" |
388 | 314 | ] |
|
399 | 325 | }, |
400 | 326 | { |
401 | 327 | "cell_type": "code", |
402 | | - "execution_count": 19, |
| 328 | + "execution_count": null, |
403 | 329 | "metadata": {}, |
404 | 330 | "outputs": [], |
405 | 331 | "source": [ |
|
413 | 339 | }, |
414 | 340 | { |
415 | 341 | "cell_type": "code", |
416 | | - "execution_count": 22, |
| 342 | + "execution_count": null, |
417 | 343 | "metadata": {}, |
418 | | - "outputs": [ |
419 | | - { |
420 | | - "name": "stdout", |
421 | | - "output_type": "stream", |
422 | | - "text": [ |
423 | | - "2178309\n" |
424 | | - ] |
425 | | - } |
426 | | - ], |
| 344 | + "outputs": [], |
427 | 345 | "source": [ |
428 | 346 | "print(fib_tail(32, 0, 1))" |
429 | 347 | ] |
430 | 348 | }, |
431 | 349 | { |
432 | 350 | "cell_type": "code", |
433 | | - "execution_count": 24, |
| 351 | + "execution_count": null, |
434 | 352 | "metadata": {}, |
435 | | - "outputs": [ |
436 | | - { |
437 | | - "data": { |
438 | | - "text/plain": [ |
439 | | - "1.3248994946479797e-05" |
440 | | - ] |
441 | | - }, |
442 | | - "execution_count": 24, |
443 | | - "metadata": {}, |
444 | | - "output_type": "execute_result" |
445 | | - } |
446 | | - ], |
| 353 | + "outputs": [], |
447 | 354 | "source": [ |
448 | 355 | "timeit(\"fib_tail(32, 0, 1)\", globals=globals(), number=1)" |
449 | 356 | ] |
450 | 357 | }, |
| 358 | + { |
| 359 | + "cell_type": "markdown", |
| 360 | + "metadata": {}, |
| 361 | + "source": [ |
| 362 | + "### Factorial definition\n", |
| 363 | + "\n", |
| 364 | + "```\n", |
| 365 | + " 1! = 1 (base case)\n", |
| 366 | + " n! = n * (n-1)! for n > 1 (general case)\n", |
| 367 | + "```\n", |
| 368 | + "\n", |
| 369 | + "- Exercise - Implement factorial recursive solution; see homework assignment!" |
| 370 | + ] |
| 371 | + }, |
451 | 372 | { |
452 | 373 | "cell_type": "markdown", |
453 | 374 | "metadata": {}, |
|
469 | 390 | }, |
470 | 391 | { |
471 | 392 | "cell_type": "code", |
472 | | - "execution_count": 3, |
| 393 | + "execution_count": null, |
473 | 394 | "metadata": {}, |
474 | | - "outputs": [ |
475 | | - { |
476 | | - "name": "stdout", |
477 | | - "output_type": "stream", |
478 | | - "text": [ |
479 | | - "Requirement already up-to-date: pip in /Users/rbasnet/miniconda3/lib/python3.7/site-packages (19.2.1)\n", |
480 | | - "Requirement already satisfied: pygame in /Users/rbasnet/miniconda3/lib/python3.7/site-packages (1.9.4)\n" |
481 | | - ] |
482 | | - } |
483 | | - ], |
| 395 | + "outputs": [], |
484 | 396 | "source": [ |
485 | 397 | "%%bash\n", |
486 | 398 | "pip install --upgrade pip\n", |
|
489 | 401 | }, |
490 | 402 | { |
491 | 403 | "cell_type": "code", |
492 | | - "execution_count": 1, |
| 404 | + "execution_count": null, |
493 | 405 | "metadata": {}, |
494 | | - "outputs": [ |
495 | | - { |
496 | | - "name": "stdout", |
497 | | - "output_type": "stream", |
498 | | - "text": [ |
499 | | - "pygame 1.9.4\n", |
500 | | - "Hello from the pygame community. https://www.pygame.org/contribute.html\n" |
501 | | - ] |
502 | | - } |
503 | | - ], |
| 406 | + "outputs": [], |
504 | 407 | "source": [ |
505 | 408 | "# animated fractal; when done just close the window or force kill if not responding!\n", |
506 | 409 | "# this program doesn't run in colab or online services. You must run locally from notebook or as a script!\n", |
|
613 | 516 | }, |
614 | 517 | { |
615 | 518 | "cell_type": "code", |
616 | | - "execution_count": 5, |
| 519 | + "execution_count": null, |
617 | 520 | "metadata": {}, |
618 | 521 | "outputs": [], |
619 | 522 | "source": [ |
|
626 | 529 | }, |
627 | 530 | { |
628 | 531 | "cell_type": "code", |
629 | | - "execution_count": 6, |
| 532 | + "execution_count": null, |
630 | 533 | "metadata": { |
631 | 534 | "scrolled": true |
632 | 535 | }, |
633 | | - "outputs": [ |
634 | | - { |
635 | | - "name": "stdout", |
636 | | - "output_type": "stream", |
637 | | - "text": [ |
638 | | - "Move disk #1 from needle1 to needle3\n", |
639 | | - "Move disk #2 from needle1 to needle2\n", |
640 | | - "Move disk #1 from needle3 to needle2\n", |
641 | | - "Move disk #3 from needle1 to needle3\n", |
642 | | - "Move disk #1 from needle2 to needle1\n", |
643 | | - "Move disk #2 from needle2 to needle3\n", |
644 | | - "Move disk #1 from needle1 to needle3\n" |
645 | | - ] |
646 | | - } |
647 | | - ], |
| 536 | + "outputs": [], |
648 | 537 | "source": [ |
649 | 538 | "moveDisks(3, 'needle1', 'needle2', 'needle3')" |
650 | 539 | ] |
|
659 | 548 | "\n", |
660 | 549 | "- the following Kattis problems can be solved using recursion:\n", |
661 | 550 | "\n", |
662 | | - "1. Watch Out For Those Hailstones! - https://open.kattis.com/problems/hailstone\n", |
663 | | - "2. Of of Sorts - https://open.kattis.com/problems/outofsorts\n", |
| 551 | + "1. Last Factorial Digit: https://open.kattis.com/problems/lastfactorialdigit\n", |
| 552 | + "2. Watch Out For Those Hailstones! - https://open.kattis.com/problems/hailstone\n", |
| 553 | + "3. Of of Sorts - https://open.kattis.com/problems/outofsorts\n", |
664 | 554 | " - Hint: implement recursive binary search and apply to the generated unsorted sequence" |
665 | 555 | ] |
666 | 556 | }, |
|
0 commit comments