Skip to content

Instantly share code, notes, and snippets.

@trhura
Created December 8, 2014 15:04
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 trhura/ceee939bfe292d1fb042 to your computer and use it in GitHub Desktop.
Save trhura/ceee939bfe292d1fb042 to your computer and use it in GitHub Desktop.
def multiply(a, b):
# Assuming a and b are not negative
if b == 0: return 0
if b == 1: return a
return a + multiply(a, b-1)
def power(b, e):
# Assuming e is not negative
if e == 0: return 1
if e == 1: return b
return multiply(b, power(b, e-1))
def random_test (func, builtin_func):
import random, sys, resource
# increment the stack depth
sys.setrecursionlimit(1000000)
# ulimit tweak
resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
for a in range (0, 7):
b = random.randint(0, 7)
assert func(a, b) == builtin_func(a, b)
import operator
random_test(multiply, operator.mul)
random_test(power, operator.pow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment