Created
September 2, 2014 01:20
-
-
Save jesuscast/975bcd57256cb69f9a47 to your computer and use it in GitHub Desktop.
Calculates the sqrt of 2 with exaggerate precision
This file contains hidden or 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
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