0 Replies - 347 Views - Last Post: 06 October 2006 - 11:31 AM

#1 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

[Scheme] Square root (newton method)

Posted 06 October 2006 - 11:31 AM

Description: provide a value to find root of, a guess as to its root and the precision to find such root.find a square root with the newton method
;square, good-enough?, and improve are shared by both a and b versions
(define (square x) ( * x x))

(define (good-enough? guess x p)
    (< (abs (- (square guess) x)) p))

(define (average x y)
    (/ (+ x y) 2))

(define (improve guess x)
    (average guess ( / x guess)))

(define (sqrt-iterationA guess x p count)
    (if(good-enough? guess x p)
            guess
            (sqrt-iterationA (improve guess x) x p (+ count 1))))

(define (sqrtA x guess precision)
    (sqrt-iterationA (* guess 1.0) x precision 0));ensure decimals


Is This A Good Question/Topic? 0
  • +

Page 1 of 1