Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Created September 2, 2014 01:20
Show Gist options
  • Save jesuscast/975bcd57256cb69f9a47 to your computer and use it in GitHub Desktop.
Save jesuscast/975bcd57256cb69f9a47 to your computer and use it in GitHub Desktop.
Calculates the sqrt of 2 with exaggerate precision
from decimal import *
getcontext().prec = 100
def powerSeries(A,N, precision):
a = Decimal(A)
n = Decimal(N)
power = Decimal(0)
total = Decimal(0)
for i in range(precision):
totalLocal = Decimal(1)
nTotal = Decimal(0)
aTotal = Decimal(1)
# Calculate nTotal
if power != Decimal(0):
nTotal = Decimal(1)
for j in range(int(power)):
nTotal = nTotal*(n-j)
powerExponential = Decimal(1)
for j in range(1,int(power)+1):
powerExponential *= j
nTotal = nTotal/powerExponential
else:
nTotal = Decimal(1)
aTotal = a**power
totalLocal = aTotal*nTotal
total += totalLocal
power = power + Decimal(1)
return total
powerSeries(1,0.5,100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment