How can one find the square root of a number without using any pre-defined functions in python?
I need the main logic of how a square root of a program works. In general math we will do it using HCF but in programing, I am not able to find the logic.
There is a famous mathematical method called Newton–Raphson method for finding successively better approximations to the roots.
Basically , this method takes initial value and then in successful iterations converges to the solution.You can read more about it here.
Sample code is attached here for your reference.
def squareRoot(n):x=ny=1.000000 #iteration initialisation.e=0.000001 #accuracy after decimal place.while x-y > e:x=(x+y)/2y=n/xprint xn = input('enter the number : ')
squareRoot(n)
Here you can increase the accuracy of square root result by adding '0' digits in e and y after decimal place.
Also there are other methods like binary search for finding square roots like shown here.