Created
January 21, 2016 14:23
-
-
Save nohe427/2ceb7d50eecb48b5cf33 to your computer and use it in GitHub Desktop.
BabylonianMethodForFindingSqrt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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