schemeracketsicp# SICP Exercise 1.8 - Why doesn't my function terminate?

I'm trying to implement Newton's method for cube roots, but my function seems to hang. My code is almost identical to the code the book shows for the square root, with the `improve`

function modified as appropriate.

Here is my code...

```
(define (cube-root x)
(cube-root-iter 1.0 x))
(define (cube-root-iter guess x)
(if (good-enough? guess x)
guess
(cube-root-iter (improve x guess) x)
)
)
(define (good-enough? guess x)
(< (abs (- (* guess guess) x)) 0.001))
(define (improve x guess)
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3)
)
```

I tested `improve`

and `good-enough?`

independently, and they seem to give the right answers. Anyone know why my code doesn't terminate?

I'm doing this in Racket with the `sicp`

package in case that makes any difference.

Solution

You want to compute the cube root. The cube root of `x`

means a value `y`

for which `y*y*y = x`

, by definition. You try to approximate `y`

using `guess`

, but you compute only `y^2`

. Need to multiply it 3 times:

```
(define (good-enough? guess x)
(< (abs (- (* guess guess guess) x))
0.001))
```

If you multiply it only 2 times, `y^2`

will never get close enough to `y^3`

, which gets closer to `x`

. If you try to compute cube root of `1`

or of `0`

with your code it might work however (and only for these inputs it might work).

- Reverse the digits of a number using a list
- Exercise 12.10 from the book Scheme and the art of programming
- How to create Alist from List using syntax-rules in Scheme?
- Anonymous lambdas directly referring to themselves
- Why can't a Scheme macro with the name "if" be defined?
- How to implement "if" in normal order in Scheme?
- Map-reduce functional outline
- How to use let-values in Gambit Scheme?
- Execute command line from Scheme (Guile)
- How do I define a sub environment in scheme?
- Sources for learning about Scheme Macros: define-syntax and syntax-rules
- What is "Call By Name"?
- DrRacket's call/cc
- Racket: Conditional Statements Not Matching String Equality in Gambling Simulation"
- What's wrong with this coin change python implementation?
- Encode "ä", "ö", "ü" and "ß" in scheme (get german text from file)
- Using AND with the apply function in Scheme
- Combination of list-ref and index-of in racket
- How to undefine a variable in Scheme?
- Running Scheme from the command line
- Function that splits word
- How does the yin-yang puzzle work?
- Dr Racket Recursing Without Returning Inital Parent Node Within Function
- Chez Scheme FFI Procedure Doesn't Work After Change to Apple Silicon
- How do we convert this Scheme (Lisp) function into C#
- What is #~ in scheme?
- Racket : Run scheme file in terminal and show result
- Recursive numeric equality in Scheme
- Under any Scheme standard, is (let (x y z) x) valid code?
- Iterate through the letters of the alphabet in Racket