This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
python3:recursion_depth [2019/05/07 12:52] jguerin Added a title. |
python3:recursion_depth [2019/05/07 13:10] (current) jguerin Modified footnote to reference "production" rather than "contests." |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Recursion Depth Limits ====== | ====== Recursion Depth Limits ====== | ||
| + | ===== Factorial ===== | ||
| + | The [[https:// | ||
| + | The following is a standard implementation derived from this definition: | ||
| + | <file python factorial.py> | ||
| + | def factorial(n): | ||
| + | if n == 0: | ||
| + | return 1 | ||
| + | else: | ||
| + | return n * factorial(n-1) | ||
| + | |||
| + | print(factorial(997)) | ||
| + | </ | ||
| + | |||
| + | In the above example //n//=997 was chosen because any larger will cause the system to throw an exception: '' | ||
| + | |||
| + | A trivial modification will reset the system recursion limit. | ||
| + | <file python factorial_deep.py> | ||
| + | import sys | ||
| + | |||
| + | sys.setrecursionlimit(2000) | ||
| + | |||
| + | def factorial(n): | ||
| + | if n == 0: | ||
| + | return 1 | ||
| + | else: | ||
| + | return n * factorial(n-1) | ||
| + | |||
| + | print(factorial(1997)) | ||
| + | </ | ||
| + | |||
| + | Note that //n//=1997 reflects the new maximum recursion depth in this example. | ||
| + | |||
| + | ===== Safety ===== | ||
| + | Note that the [[https:// | ||