def f(x): """pointless sample function""" if x < 0: return 0 else: x-=1 return f(x) def power(x,y,tot=1): """raise to the power - no square optimisation (lisp-like)""" if y > 0: tot = x*tot return power(x,y-1,tot) else: return 1 # # Note this would be faster if the optimisation removed # the unneccessary loading and saving of "x" # before each loop # def quickpower(x, y): """raise to the power - with square optimisation""" if y%2 == 1: return quickpower( quickpower(x,(y-1)/2), 2) elif y == 2: return x*x elif y > 0: return quickpower( quickpower(x,y//2) , 2 ) else: return 1 def gcd(a,b): """greatest common devisor - Euclid""" if b > a: c=a a=b b=a if b > 0: return gcd(b,a%b) return a def gcd_dijkstra(m, n): """greatest common devisor - Dijkstra""" if m > n: return gcd_dijkstra(m-n, n) elif n > m: return gcd_dijkstra(m, n-m) return m def fact(x,tot=1): """Factorial (lisp-like)""" if x > 1: return fact(x-1,tot*x) return tot # This could be much faster because # data and search_for are not modified # But we still end up doing # # LOAD_FAST data # LOAD_FAST search_for # ... # SAVE_FAST search_for # SAVE_FAST data # # before doing our # JUMP_ABSOLUTE