Skip to content

Instantly share code, notes, and snippets.

@ethe
Last active April 3, 2016 15:56
Show Gist options
  • Select an option

  • Save ethe/f0fc6a04348390f9ec79fa5dd523281a to your computer and use it in GitHub Desktop.

Select an option

Save ethe/f0fc6a04348390f9ec79fa5dd523281a to your computer and use it in GitHub Desktop.
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