|
SketchyLISP Reference |
Copyright (C) 2007 Nils M Holm |
| <<[reverse] | [Index] | [string]>> |
Conformance: R5RS Scheme (Restrictions: Result is an integer. )
Purpose: Compute the square root of a natural number. (In fact, this function computes the greatest integer that is no greater than the square root of the given argument.)
Arguments:
X - square of root to extract
Implementation:
(define (sqrt square)
(letrec
((_sqrt (lambda (x last)
(cond ((= last x) x)
((= last (n+ 1 x))
(if (n> (n* x x) square) (- x 1) x))
(else (_sqrt (nquotient
(n+ x (nquotient square x))
2)
x))))))
(cond ((negative? square)
(bottom (list 'sqrt square)))
(else (_sqrt square 0)))))
Example:
(sqrt 144) => 12
| <<[reverse] | [Index] | [string]>> |