please code in python Implement this digit-by-digit algorithm to find the square root of x to a precision of epsilon. Begin with a step size of 1 and guess of zero. Increase the guess by the step size as long as doing so would not cause the guess^2 to exceed x. Repeat this step (step 2) until the next repetition will cause guess^2 to exceed x. If the step size is greater than or equal to epsilon, then divide the step size by 10 and go back to step 2.
please code in python
Implement this digit-by-digit
- Begin with a step size of 1 and guess of zero.
- Increase the guess by the step size as long as doing so would not cause the guess^2 to exceed x. Repeat this step (step 2) until the next repetition will cause guess^2 to exceed x.
- If the step size is greater than or equal to epsilon, then divide the step size by 10 and go back to step 2.
Notice that once a digit has been found, it is not changed again. Try using your code to find the square root of 10 to an epsilon of 8 decimal places. This algorithm should not take over 10 seconds to run (if it does you have coded something incorrectly!).
If you are getting an answer that is off by epsilon (for example 3.16227767 instead of 3.16227766), this might be caused by the floating point error. You can read more about that here: https://docs.python.org/3/tutorial/floatingpoint.html. To fix this problem, you can use the Decimal library here: https://docs.python.org/3/library/decimal.html#module-decimal
Hint: It might be helpful to write out by hand an easier example like '4' to understand how the algorithm works.
Example output:
Enter a number to find the square root: 10
The square root of 10 is: 3.16227766
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images