C:
#include <stdio.h>
#define Zero 1e-6
float mysqrt(int number) {
float guess = 1.0f;
float quotient = number/guess;
float average = (guess + quotient)/2.0;
do {
guess = average;
quotient = number/guess;
average = (guess + quotient)/2.0;
printf("%f\n", guess);
}while (guess*guess - number >= Zero);
return guess;
}
int main() {
int number;
scanf("%d", &number);
printf(" your sqrt is %f", mysqrt(number));
return 0;
}
scheme:
(define (improve guess x)
(average guess (/ x guess)))
(define (goodenough guess x)
(< (abs (- (square guess) x) 0.00001))
(define (sqrt-iter guess x)
(if (goodenough guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (mymsqrt x)
(sqrt-iter 1.0 x))
(mysqrt 9)