Sep 6, 2017 05:50 PM
(This post was last modified: Sep 6, 2017 05:57 PM by SIM-2306826.)
So, I'm dabbling in to Python a bit more and my professor mentioned something about Heron's Algorithm. I thought it was interesting so I made a little concept based on his psuedo-code.
This basically will keep iterating until it finds the perfect square root of the number you input. It starts off by taking dividing the root by your guess, adding your guess to that, and then dividing that whole thing by two, to find the average. It then creates that as your new guess and the whole process keeps going.
See below:
Running this, we get something like this:
This basically will keep iterating until it finds the perfect square root of the number you input. It starts off by taking dividing the root by your guess, adding your guess to that, and then dividing that whole thing by two, to find the average. It then creates that as your new guess and the whole process keeps going.
See below:
Code
intSquare = int(input("Please enter the square root find the answer of: "))
intGuess = float(input("Please enter the square root guess: "))
intApprox = abs((intGuess + intSquare/intGuess)/2.)
resFound = False
numTry = 0
while not resFound:
numTry += 1
prevApprox = intApprox
print("Guess number " + str(numTry) + " is " + str(intApprox))
intApprox = 1/2 * (intApprox + intSquare/intApprox)
if (intApprox - prevApprox) == 0:
resFound = True
print("Using only " + str(numTry) + " iterations, the actual square root was found to be " + str(intApprox))Running this, we get something like this:


![[Image: ouURNlq.png]](https://i.imgur.com/ouURNlq.png)