Last active
April 3, 2016 15:56
-
-
Save ethe/f0fc6a04348390f9ec79fa5dd523281a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fact(n): | |
| if n == 0: | |
| return 1 | |
| else: | |
| return n * fact(n - 1) | |
| def fact_cps(n, k=lambda x: x): | |
| if n == 0: | |
| return k(1) | |
| else: | |
| return fact_cps(n-1, lambda x: k(n * x)) | |
| def fact_loop(n, k=lambda x: x): | |
| def factory(n, k): | |
| return lambda x: k(n * x) | |
| while True: | |
| if n == 0: | |
| return k(1) | |
| else: | |
| k = factory(n, k) | |
| n -= 1 | |
| def fact_with_stack(n, k=()): | |
| while True: | |
| if n == 0: | |
| x = 1 | |
| while k: | |
| x = k[0] * x | |
| k = k[1] | |
| return x | |
| else: | |
| k = (n, k) | |
| n -= 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment