**Exercise 4.2** In this exercise you will show that this version of mod-expt does multiplications, as we claimed.

**a.** Use induction to prove each of the following about this latest version of mod-expt:

**(1)** e is a nonnegative integer, (mod-expt b e m) does at least e multiplications.

**(2)** When e is a positive integer, (mod-expt b e m) does at most 2e – 1 multiplications.

**Solution:** At first I wrote the function mathematically.

**(1)** Ok, now let’s start with the first assumption: m(b, e, m) does at least e multiplications. For follows that So that’s true. Therefore we can assume that this holds to

If is odd:

, we assumed that does multiplications. If we look back at the definition of , we can see that there’s one multiplication. Therefore there are multiplications. That was the best case, let’s look at the worst one.

**(2)** The assumption is that needs multiplications at most. Let’s check the base case for . Here we get one multiplication from , so the maximum limit holds. If is even, it follows that:

Each nested needs at most multiplications and we got another from . That is, there are multiplications.

You can check the even case in (1) and the odd case in (2) for yourself and see that these are bigger and respectively smaller than the other ones.

**Exercise 4.11** Consider the following procedures:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) (define factorial-sum1 ; returns 1! + 2! + … + n! (lambda (n) (if (= n 0) 0 (+ (factorial n) (factorial-sum1 (- n 1)))))) (define factorial-sum2 ; also returns 1! + 2! + … + n! (lambda (n) (define loop (lambda (k fact-k addend) (if (> k n) addend (loop (+ k 1) (* fact-k (+ k 1)) (+ addend fact-k))))) (loop 1 1 0))) |

In answering the following, assume that n is a nonnegative integer. Also, justify your answers.

**a.** Give a formula for how many multiplications the procedure factorial does as a function of its argument n.

**b.** Give a formula for how many multiplications the procedure factorial-sum1 does (implicitly through factorial) as a function of its argument n.

**c.** Give a formula for how many multiplications the procedure factorial-sum2 does as a function of its argument n.

**Solution:**

**a.**

What happens for ? Let’s see:

We can see that there are probably multiplications. We can prove that by induction.

. We assumed that multiplies times. Therefore we have multiplications.

**b.** And again written mathematically:

So was happens here here for ?

For we need 2 multiplications, for we need multiplications. In general we need multiplications. This can be simplified:

**c.** This is pretty straight forward. You can see that each iteration k is increased by one. It starts at k = 1 and stops if k > n. So it runs n times.

**Exercise 4.12** How many ways are there to factor n into two or more numbers (each of which must be no smaller than 2)? We could generalize this to the problem of finding how many ways there are to factor n into two or more numbers, each of which is no smaller than m. That is, we write

1 2 3 |
(define ways-to-factor (lambda (n) (ways-to-factor-using-no-smaller-than n 2))) |

Your job is to write ways-to-factor-using-no-smaller-than.

**Solution:** An easy way to find factors is just to try every number if it divides n, starting with m. If a number divides n, take the division and find its factors.

1 2 3 4 5 6 7 8 9 10 |
(define ways-to-factor-using-no-smaller-than (lambda (n m) (define factor (lambda (x y) (if (> y (sqrt x)) 0 (if (= (remainder x y) 0) (+ 1 (factor (/ x y) m)) (factor x (+ y 1)))))) (+ 1 (factor n m)))) |

**Exercise 4.13** Consider the following procedure:

1 2 3 4 5 |
(define bar (lambda (n) (cond ((= n 0) 5) ((= n 1) 7) (else (* n (bar (- n 2))))))) |

How many multiplications (expressed in notation) will the computation of (bar n) do? Justify your answer. You may assume that n is a nonnegative integer.

**Solution:** The definition of bar is:

For we get:

You can see that there are between and multiplications. Therefore there are multiplications at general.

**Exercise 4.14 **Consider the following procedures:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
(define foo (lambda (n) ; computes n! + (n!)^n (+ (factorial n) ; that is, (n! plus n! to the nth power) (bar n n)))) (define bar (lambda (i j) ; computes (i!)^j (i! to the jth power) (if (= j 0) 1 (* (factorial i) (bar i (- j 1)))))) (define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) |

How many multiplications (expressed in notation) will the computation of (foo n) do? Justify your answer.

**Solution:** It’s basically the same schema as the other exercises. If we look at foo it looks like this:

We already know that needs multiplications. If we look at bar, we can see how it works.

we can simplify this because foo which calls bar only uses the argument n.

Because we already know how many multiplications fac needs, we can concentrate on bar. Let’s see what it does for :

We can generalize this as which can be simplified as: therefore we have multiplications.

**Exercise 4.17** Consider the following enumeration problem: How many ways can you choose k objects from n distinct objects, assuming of course that ? For example, how many different three-topping pizzas can be made if you have six toppings to choose from?

The number that is the answer to the problem is commonly written as C(n, k). Here is an algorithm for computing C(n, k): […]

Using this algorithm, write a tree-recursive procedure that calculates the numbers C(n, k) described above.

**Solution:**

1 2 3 4 5 6 |
(define C (lambda (n k) (if (or (= k 0) (= k n)) 1 (+ (C (- n 1) (- k 1)) (C (- n 1) k))))) |

**Exercise 4.18** One way to sum the integers from a up to b is to divide the interval in half, recursively sum the two halves, and then add the two sums together. Of course, it may not be possible to divide the interval exactly in half if there are an odd number of integers in the interval. In this case, the interval can be divided as nearly in half as possible.

**a.** Write a procedure implementing this idea.

**b.** Let’s use n as a name for the number of integers in the range from a up to b. What is the order of growth (in notation) of the number of additions your procedure does, as a function of n? Justify your answer.

**Solution:** **a.**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
(define sum-integers (lambda (a b) (+ a (divide-to-sum a b)))) (define divide-to-sum (lambda (a b) (let ((interval (- b a))) (if (< interval 2) b (if (even? interval) (+ (divide-to-sum a (+ a (/ interval 2))) (divide-to-sum (+ a (/ interval 2)) b)) (+ (divide-to-sum a (+ a (/ (+ interval 1) 2))) (divide-to-sum (+ a (/ (+ interval 1) 2)) b))))))) |

**b.** It’s basically the same as in mod-expt because both split up the calculation in two branches each time. Therefore it needs additions.