am_dot_com

AI / IA 2022-10-19

Oct 19th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # 1.py
  2. import math
  3.  
  4. DEFAULT_STARTING_NUMBER = 999
  5. DEFAULT_PRECISION = 1/1000
  6.  
  7. def heronOfAlexandriaAlgoForSR(
  8. pX, # I want to know the square root of
  9. pStartingGuess = DEFAULT_STARTING_NUMBER,
  10. pPrecision = DEFAULT_PRECISION,
  11. pbVerbose:bool = True
  12. ):
  13. guess = pStartingGuess
  14. distance = guess**2 - pX
  15. distance = math.fabs(distance)
  16. bGoodEnough:bool = distance <= pPrecision
  17.  
  18. iIteration:int = 1
  19. while(not bGoodEnough):
  20. iIteration += 1
  21. guess = (guess + pX/guess)/2
  22. distance = guess ** 2 - pX
  23. distance = math.fabs(distance)
  24. bGoodEnough: bool = distance <= pPrecision
  25.  
  26. strMsg = f"Iteration:{iIteration}\n"
  27. strMsg += f"Current guess: {guess}\n"
  28. strMsg += f"Current distance: {distance}\n"
  29. if (pbVerbose):
  30. print(strMsg)
  31. # if
  32. # while
  33. # guess is the square of pX
  34. return guess
  35. # def heronOfAlexandriaAlgoForSR
Advertisement
Add Comment
Please, Sign In to add comment