Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1.py
- import math
- DEFAULT_STARTING_NUMBER = 999
- DEFAULT_PRECISION = 1/1000
- def heronOfAlexandriaAlgoForSR(
- pX, # I want to know the square root of
- pStartingGuess = DEFAULT_STARTING_NUMBER,
- pPrecision = DEFAULT_PRECISION,
- pbVerbose:bool = True
- ):
- guess = pStartingGuess
- distance = guess**2 - pX
- distance = math.fabs(distance)
- bGoodEnough:bool = distance <= pPrecision
- iIteration:int = 1
- while(not bGoodEnough):
- iIteration += 1
- guess = (guess + pX/guess)/2
- distance = guess ** 2 - pX
- distance = math.fabs(distance)
- bGoodEnough: bool = distance <= pPrecision
- strMsg = f"Iteration:{iIteration}\n"
- strMsg += f"Current guess: {guess}\n"
- strMsg += f"Current distance: {distance}\n"
- if (pbVerbose):
- print(strMsg)
- # if
- # while
- # guess is the square of pX
- return guess
- # def heronOfAlexandriaAlgoForSR
Advertisement
Add Comment
Please, Sign In to add comment