def sum_naturals(n): """Sum the first N natural numbers. >>> sum_naturals(5) 15 """ total, k = 0, 1 while k <= n: total, k = total + k, k + 1 return total def sum_cubes(n): """Sum the first N cubes of natural numbers. >>> sum_cubes(5) 225 """ total, k = 0, 1 while k <= n: total, k = total + pow(k, 3), k + 1 return total identity = lambda k: k cube = lambda k: pow(k, 3) def summation(n, term): """Sum the first N terms of a sequence. >>> summation(5, cube) 225 """ total, k = 0, 1 while k <= n: total, k = total + term(k), k + 1 return total def sum_naturals(n): return summation(n, identity) def sum_cubes(n): return summation(n, cube) f = lambda k: 8 / (4 * k - 1) / (4 * k - 3) summation(1000000, f) def make_adder(n): """Return a function that takes one argument K and returns K + N. >>> add_three = make_adder(3) >>> add_three(4) 7 """ def adder(k): return k + n return adder def square(x): return x * x def triple(x): return 3 * x def compose1(f, g): """Return a function that composes f and g. f, g -- functions of a single argument """ def h(x): return f(g(x)) return h squiple = compose1(square, triple) tripare = compose1(triple, square) squadder = compose1(square, make_adder(2)) squiple(5) # 225 tripare(5) # 75 squadder(5) # 49 (lambda x, y: x * y + 1)(3, 4) (lambda x: lambda y: x * y + 1)(3)(4)