Saturday, August 21, 2010

SICP 1.45: Computing nth roots

From SICP section 1.3.4 Procedures as Returned Values

Exercise 1.45 asks us to do some experiments to find out how many average damps are needed to compute nth roots as a fixed-point search based on repeated average damping of y → x / yn-1. Once we know that, we need to write a general procedure for finding nth roots using fixed-point, average-damp, and the repeated procedure from exercise 1.43.

We already saw in section 1.3.3 that computing a square root by finding a fixed point of y → y/x doesn't converge, and that this can be fixed by average damping (see exercise 1.36). The same method (a single average damp) works for finding cube roots as fixed points of y → x / y2.

Here's a first attempt at an nth-root procedure, which works when n is 2 or 3.
(define (nth-root x n)
(lambda (y) (/ x (expt y (- n 1)))))

> (nth-root 100 2)
> (nth-root 1000 3)

The same procedure does not work for fourth roots. Try running nth-root with the following parameters and you'll see that the procedure never finishes.
> (nth-root 10000 4)

This is because the fixed-point search for y → x / y3 doesn't converge when only one average damp is used. We can fix this by damping the average twice.
(define (nth-root x n)
((repeated average-damp 2)
(lambda (y) (/ x (expt y (- n 1)))))

We'll need to run the new procedure several times to find out where average damping twice fails to converge. The code above works for finding nth roots when n is less than 8.
> (nth-root 10000 4)
> (nth-root 100000 5)
> (nth-root 1000000 6)
> (nth-root 10000000 7)

Let's increase the repetitions of average-damp one more time and see if we can spot a pattern.
(define (nth-root x n)
((repeated average-damp 3)
(lambda (y) (/ x (expt y (- n 1)))))

> (nth-root 256 8)
> (nth-root 512 9)
> (nth-root 1024 10)
> (nth-root 2048 11)
> (nth-root 4096 12)
> (nth-root 8192 13)
> (nth-root 16384 14)
> (nth-root 32768 15)

When we average damp 3 times, the nth-root procedure converges for n up to 15. That worked longer than I expected, but it gives us enough information to see the pattern. When n (roughly) doubles, we need to increase the number of average damps by one.

maximum n: 3, 7, 15
average damps: 1, 2, 3

Where a is the number of average damps, we can say

nmax = 2a+1 - 1

We can test this by checking to see that by increasing the number of average damps to four, our nth-root procedure works for values of n up to 31.
(define (nth-root x n)
((repeated average-damp 4)
(lambda (y) (/ x (expt y (- n 1)))))

> (nth-root 2147483648 31)

Just as expected, the procedure fails to converge if we run it with n = 32. I'm convinced that this is the right pattern.

In order to calculate the number of average damps from n, we just need to take the log2 of n then floor the result. Since Scheme only has a log procedure that finds logarithms in base e, this must be what the authors meant when they said in the exercise "Assume that any arithmetic operations you need are available as primitives."

It's easy enough to write a procedure to compute log2 if we just remember that

logn(x) = log(x) / log(n)

(define (log2 x)
(/ (log x) (log 2)))

> (log2 16)
> (log2 64)
> (log2 65536)

With that we can fix the nth-root procedure so it works for any value of n.
(define (nth-root x n)
((repeated average-damp (floor (log2 n)))
(lambda (y) (/ x (expt y (- n 1)))))

> (nth-root 4294967296 32)
> (nth-root 18446744073709551616 64)
> (nth-root 340282366920938463463374607431768211456 128)


For links to all of the SICP lecture notes and exercises that I've done so far, see The SICP Challenge.


Anonymous said...


Did you try using 3 average damps beyond 15? 3 average damps works all the way up to 31-roots. For 32-roots and beyond, 4 average damps are required.

In fact, 4 average damps seems to work fine for 64-roots, 128-roots, 256-roots, and so on.

Actually, 3 average damps works for 512-roots too. Perhaps I skipped over the n-root where 4 average damps first fails.

Anonymous said...

It seems that if the index any integer
2^n. it works with less damps. Nice thing with the 'floor' procedure, I didn't know about it, and wrote a little procedure to
give me the integer lesser than the log2.

Anonymous said...

For some reason I don't understand in my computer the forth root does converge with just one average damp. Any idea why this could be happening?