The Babylonian algorithm for computing the square root of a number is a simple i
ID: 3811823 • Letter: T
Question
The Babylonian algorithm for computing the square root of a number is a simple iterative technique that can be surprisingly accurate: (This algorithm is also known as a ‘Taylor Series’ to compute the square root.)
x_n+1=1/2(x_n+S/x_n)
Where:
S is the number you wish to find the square root of;
xn is the current guess for the square root of S; and
xn+1 is the updated (computed), next guess for the square root of S.
n is the iteration step number
We will refer to the above formula as “Formula 1”.
If we let x0 be our initial guess for the square root of S in Formula 1, and plug it in for xn on the right hand side of Formula 1 (i.e., let n = 0), then we can compute x1, the first, updated (computed) guess for the square root of S, on the left hand side of Formula 1:
x_1=1/2(x_o+S/x_0
And now, if we use x1 that we just computed on the right hand side of Formula 1 (i.e., now let n = 1), then we can compute x2, the second, updated guess for the square root of S:
x_2=1/2(x_1+S/x_1
So x2 will be the second, updated guess for the square root of S.
In a similar way, we can keep using Formula 1, in an iterative fashion, to compute the third (x3), fourth (x4), fifth (x5) . . . updated guesses for the square root of S. Each updated guess gets closer and closer to the actual value of the square root of S, and each updated guess calculation with Formula 1 uses as right hand side input the value of the previously updated guess, as shown above.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void squareRoot(int S, double guess)
{
double nextguess;
int i;
int n;
printf("Enter n is the iteration step number : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
nextguess = (guess+S/guess)/2;
printf(" Iteration %d then Next guess is : %lf",i+1,nextguess);
guess=nextguess;
}
}
/* Driver program to test above function*/
int main()
{
int s;
double guess;
printf ("Enter S is the number you wish to find the square root of : ");
scanf("%d",&s);
printf("Enter the current guess for the square root of S : ");
scanf("%lf",&guess);
squareRoot(s,guess);
return;
}
--------------------------------------------
output sample 1:-
Enter S is the number you wish to find the square root of : 64
Enter the current guess for the square root of S : 4
Enter n is the iteration step number : 5
Iteration 1 then Next guess is : 10.000000
Iteration 2 then Next guess is : 8.200000
Iteration 3 then Next guess is : 8.002439
Iteration 4 then Next guess is : 8.000000
Iteration 5 then Next guess is : 8.000000
----------------------------------------------------------------------------
output sample 2:-
Enter S is the number you wish to find the square root of : 25
Enter the current guess for the square root of S : 2
Enter n is the iteration step number : 6
Iteration 1 then Next guess is : 7.250000
Iteration 2 then Next guess is : 5.349138
Iteration 3 then Next guess is : 5.011394
Iteration 4 then Next guess is : 5.000013
Iteration 5 then Next guess is : 5.000000
Iteration 6 then Next guess is : 5.000000
---------------------------------------------------------------------------------
output sample 3:-
Enter S is the number you wish to find the square root of : 100
Enter the current guess for the square root of S : 6
Enter n is the iteration step number : 6
Iteration 1 then Next guess is : 11.333333
Iteration 2 then Next guess is : 10.078431
Iteration 3 then Next guess is : 10.000305
Iteration 4 then Next guess is : 10.000000
Iteration 5 then Next guess is : 10.000000
Iteration 6 then Next guess is : 10.000000
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.