def sum(term, a, next, b): logl("(" + str(a) + ", " + str(b) + ")") if a > b: return 0 else: return term(a) + sum(term, next(a), next, b)
def f_recu(n): logl("(" + str(n) + ")") if n < 3: return n else: return f_recu(n - 1) + 2 * f_recu(n - 2) + 3 * f_recu(n - 3)
def expt_mult(a, b): logl("(" + str(a) + ", " + str(b) + ")") if b == 0: return 0 else: return a + expt_mult(a, b - 1)
def iter(a, result): logl("(" + str(a) + ", " + str(result) + ")") if a > b: return result else: return iter(next(a), combiner(a, result))
def sine(angle): logl("(" + str(angle) + ")") if not (abs(angle) > 0.1): return angle else: return p(sine(angle / 3.0))
def factorial(n): logl("(" + str(n) + ")") if n == 1: return 1 else: return n * factorial(n - 1)
def gcd(a, b): logl("(" + str(a) + ", " + str(b) + ")") if b == 0: return a else: return gcd(b, a % b)
def iter(i, x): logl("(" + str(i) + ", " + str(x) + ")") if i == 0: return x return iter(i - 1, n(i) / (d(i) + x))
def recu(i): logl("(" + str(i) + ")") if i > k: return 0 return n(i) / (d(i) + recu(i + 1))
def prod_recu(term, a, next, b): logl("(" + str(a) + ", " + str(b) + ")") if a > b: return 1 else: return term(a) * prod_recu(term, next(a), next, b)
def expt_iter(b, counter, product): logl("(" + str(b) + ", " + str(counter) + ", " + str(product) + ")") if counter == 0: return product else: return expt_iter(b, counter - 1, b * product)
def accu_recu(combiner, null_value, term, a, next, b): logl("(" + str(a) + ", " + str(b) + ")") if a > b: return null_value else: return combiner( term(a), accu_recu(combiner, null_value, term, next(a), next, b))
def fact_iter(product, counter, max_count): logl("(" + str(product) + ", " + str(counter) + ", " + str(max_count) + ")") if counter > max_count: return product else: return fact_iter(counter * product, counter + 1, max_count)
def fast_expt(b, n): logl("(" + str(b) + ", " + str(n) + ")") if n == 0: return 1 elif even(n): return square(fast_expt(b, n / 2)) else: return b * fast_expt(b, n - 1)
def expt_iter(a, b, n): logl("(" + str(a) + ", " + str(b) + ", " + str(n) + ")") if n == 0: return a elif even(n): return expt_iter(a, square(b), n / 2) else: return expt_iter(a * b, b, n - 1)
def expmod(base, exp, m): logl("(" + str(base) + ", " + str(exp) + ", " + str(m) + ")") if exp == 0: return 1 elif even(exp): return square(expmod(base, exp / 2, m)) % m else: return base * expmod(base, exp - 1, m) % m
def mult_iter(r, a, b): logl("(" + str(r) + ", " + str(a) + ", " + str(b) + ")") if b == 0: return r elif even(b): return mult_iter(r, double(a), halve(b)) else: return mult_iter(r + a, a, b - 1)
def fast_prime(n, times): logl("(" + str(n) + ", " + str(times) + ")") if times == 0: return True elif fermat_test(n): return fast_prime(n, times - 1) else: return False
def find_divisor(n, test_divisor): logl("(" + str(n) + ", " + str(test_divisor) + ")") if square(test_divisor) > n: return n elif divides(test_divisor, n): return test_divisor else: return find_divisor(n, test_divisor + 1)
def fast_expt(a, b): logl("(" + str(a) + ", " + str(b) + ")") if b == 0: return 0 elif even(b): return double(fast_expt(a, halve(b))) else: return a + fast_expt(a, b - 1)
def f_iter(a, b, c, i, n): logl("(" + str(a) + ", " + str(b) + ", " + str(c) + ", " + str(i) + ", " + str(n) + ")") if n < 3: return n elif i > n: return c else: return f_iter(b, c, c + 2 * b + 3 * a, i + 1, n)
def A(x, y): logl("(" + str(x) + ", " + str(y) + ")") if y == 0: return 0 elif x == 0: return 2 * y elif y == 1: return 2 else: return A(x - 1, A(x, y - 1))
def cc(amount, kinds_of_coins): logl("(" + str(amount) + ", " + str(kinds_of_coins) + ")") if amount == 0: return 1 elif amount < 0 or kinds_of_coins == 0: return 0 else: return ( cc(amount, kinds_of_coins - 1) + cc(amount - first_denomination(kinds_of_coins), kinds_of_coins))
def fib_iter(a, b, p, q, count): logl("(" + str(a) + ", " + str(b) + ", " + str(p) + ", " + str(q) + ", " + str(count) + ")") if count == 0: return b elif even(count): return fib_iter(a, b, square(p) + square(q), 2 * p * q + square(q), count / 2) else: return fib_iter((b * q) + (a * q) + (a * p), (b * p) + (a * q), p, q, (count - 1))
def filtered_accumulate(filter, combiner, null_value, term, a, next, b): logl("(" + str(a) + ", " + str(b) + ")") if a > b: return null_value elif filter(a): return combiner( term(a), filtered_accumulate(filter, combiner, null_value, term, next(a), next, b)) else: return filtered_accumulate(filter, combiner, null_value, term, next(a), next, b)
def sum_of_nodes(sum, i, n): logl("(" + str(sum) + ", " + str(i) + ", " + str(n) + ")") def node(i, n): if i == 1 or i == n: return 1 else: return node(i - 1, n - 1) + node(i, n - 1) if n == 0: return sum elif i == 0: return sum_of_nodes(sum, n - 1, n - 1) else: return sum_of_nodes(sum + node(i, n), i - 1, n)