Exercise 1.9.
The first one is a lineair recursive process and the second one is a lineair iterative process.
Exercise 1.10.
(A 1 10) = 1024
(A 2 4) = 65536
(A 3 3) = 65536
(f n) computes 2n
(g n) computes 2^n
(h n) computes 2^h(n-1)
Exercise 1.14.
Draw the tree. Done. 12 ways to return the money. The space and time complexity are O(amount * num-coins).
Exercise 1.15.
When sine is called, the angle is divided by 3. When the angle is smaller than 0.1 the procedure stops calling himself. 12.15 / 3^5 = 0.05 so the answer is 5 times.
angle/3^n=0.1 => Space and time complexity are O(log(angle))
Exercise 1.20.
When a normal-order evaluation is used, remainder is evaluated 18 times. With application-order evaluation only 4 times.
Exercise 1.21.
199, 1999 and 7. This means that the first 2 are prime.
*Exercise 1.22.
I used a bit larger numbers so the timing data would make more sense. I used the average of the timings of the first 3 prime numbers.
10^10 : 120ms
10^11 : 406ms -> ratio with previous: 3.37
10^12 : 1346ms -> 3.32
10^13 : 4212ms -> 3.13
sqrt(10) = 3.16 so the run time is proportional to the number of steps required for the computation.
Exercise 1.23.
Timings with the modified version of smallest-divisor, again with averages:
10^10 : 63ms -> old/new: 1.90
10^11 : 239ms -> 1.70
10^12 : 707ms -> 1.90
10^13 : 2267ms -> 1.85
The new version almost runs twice as fast. Maybe because there are some constants that are not affected by the change we made. Also the new procedure creates some overhead.
Exercise 1.24.
I chose to let the fast-prime? procedure test 500 times. For small values the ratio was pretty poor compared to the other prime test. But the timing increased slow when the range increased. The procedure is logarithmic due to the expmod procedure.
Exercise 1.25.
The procedure is correct but it will not be as efficient because there will be larger intermediate values. So it will take longer to compute for example the product.
Exercise 1.26.
If expmod is implemented like this, then it's tree recursion.