Skip to content

Instantly share code, notes, and snippets.

@nohe427
Created January 21, 2016 14:23
Show Gist options
  • Save nohe427/2ceb7d50eecb48b5cf33 to your computer and use it in GitHub Desktop.
Save nohe427/2ceb7d50eecb48b5cf33 to your computer and use it in GitHub Desktop.
BabylonianMethodForFindingSqrt
class alexMath(object):
"""Demonstaration of how the computer calculates the square roots of
numbers. Shows all iterations of what it calculates for example on
how it works.
Based off of the Babylonian Method located here:
https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method"""
@staticmethod
def __privSqrtFunc(number, sigdigs):
number = float(number)
sigdigs = float(sigdigs)
z = float(0.5*(sigdigs+(number/sigdigs)))
return z
@staticmethod
def sqrtFunc(numb, sigs=0):
if (sigs <= 1):
sigs = 1
numb = float(numb)
sigs = float(sigs)*float(sigs)
while(sigs!=alexMath.__privSqrtFunc(numb, sigs)):
sigs = alexMath.__privSqrtFunc(numb,sigs)
print(sigs)
return sigs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment