calculate sqrt using nested intervals in c

79 Views Asked by At

I have to write a script in C where the sqrt of an positive number is calculated using nested intervals and call by reference (no structs or math.h allowed)

I tried for like 5 days but I'm kina depressed now. Can anyone help?

1

There are 1 best solutions below

0
Dominique On

I can show you how to calculate the square root of 5. From there you'll know what to do: First, you know that the square root of a number is generally smaller than the number itself, so you start with the interval [0,5], and you calculate the middle (which is 2.5, whose square is 6.25. So, the square root of 5 should be between 0 and 2.5. So you get:

interval : [0,     2.5]. Middle=1.25.  Square(middle)=1.5625. Square(middle)<5 => this becomes the lower bound:
interval : [1.25,  2.5]. Middle=1.875. Square(middle)=±3.5.   Square(middle)<5 => this becomes the lower bound:
interval : [1.875, 2.5]. ...