Skip to content

Instantly share code, notes, and snippets.

@ianb
Created October 25, 2012 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianb/3950268 to your computer and use it in GitHub Desktop.
Save ianb/3950268 to your computer and use it in GitHub Desktop.
import threading
try:
from Queue import Queue
except ImportError:
from queue import Queue
def fact(n, queue=None):
if n == 0 or n == 1:
if queue:
queue.put(1)
return
else:
return 1
q = Queue()
t = threading.Thread(target=fact, args=(n - 1, q))
t.start()
result = q.get() * n
if queue:
queue.put(result)
return
else:
return result
if __name__ == "__main__":
import sys
print (fact(int(sys.argv[1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment