D
Devon_Nullman
Guest
I tried the Nilakantha Method - very slow and maxes out at 24 significant digits - Precision of Double Number Type
I found this Python Code that is very fast and easily gets 100 significant digits:
#Python Code
from math import factorial
from decimal import Decimal, getcontext
# Chudnovsky algorithm for figuring out pi
getcontext().prec=200
# No equivalent in .NET
pi_input = input("How many iterations would you like?")
#15 used here works very well
n = int(pi_input)
t= Decimal(0)
pi = Decimal(0)
deno= Decimal(0)
for k in range:
t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
pi += Decimal(t)/Decimal(deno)
pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
pi = 1/pi
print (pi)
dummy = input('Press Enter')
#End Python Code
I translated to VB NET as follows
Function Factorial(ByVal number As Double) As Double
If number <= 1 Then
Return (1)
Else
Return number * Factorial(number - 1)
End If
End Function
For k = 0 To n - 1
'for k in range:
t = Math.Pow(-1, k) * Factorial(6 * k) * (13591409 + 545140134 * k)
't = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
deno = Factorial(3 * k) * Math.Pow(Factorial(k), 3) * Math.Pow(640320, 3 * k)' exception here - Value too large or too small for Decimal
'deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
pi += t / deno
Next
'end of for k in range:
pi = (pi * 12) / (Math.Pow(640320, 1.5))
'pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
pi = 1 / pi
'pi = 1/pi
The remainder of the console code just prints the result
If I use Decimal I get an exception at the deno = line (Value is too large or too small for a Decimal)
Sample run of the Python Code
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
How many iterations would you like?15
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303820
Press Enter
Actual Published Value of Pi
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819
Sample from VB ,NET code
3.1415926535897935600871733186068013310432434082031250000000000000 - this
3.1415926535897932384626433832795028841971693993751058209749445923 - known
Am I converting wrong or is it just that the decimal type in python is more precise and handles larger and smaller values than the .NET decimal / Double ?
Python "x ** y" is Math.Pow(x,y)
Python getcontext().prec=200 has no equivalent
Python for k in range: is "for k = 0 to n-1
Continue reading...
I found this Python Code that is very fast and easily gets 100 significant digits:
#Python Code
from math import factorial
from decimal import Decimal, getcontext
# Chudnovsky algorithm for figuring out pi
getcontext().prec=200
# No equivalent in .NET
pi_input = input("How many iterations would you like?")
#15 used here works very well
n = int(pi_input)
t= Decimal(0)
pi = Decimal(0)
deno= Decimal(0)
for k in range:
t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
pi += Decimal(t)/Decimal(deno)
pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
pi = 1/pi
print (pi)
dummy = input('Press Enter')
#End Python Code
I translated to VB NET as follows
Function Factorial(ByVal number As Double) As Double
If number <= 1 Then
Return (1)
Else
Return number * Factorial(number - 1)
End If
End Function
For k = 0 To n - 1
'for k in range:
t = Math.Pow(-1, k) * Factorial(6 * k) * (13591409 + 545140134 * k)
't = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
deno = Factorial(3 * k) * Math.Pow(Factorial(k), 3) * Math.Pow(640320, 3 * k)' exception here - Value too large or too small for Decimal
'deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
pi += t / deno
Next
'end of for k in range:
pi = (pi * 12) / (Math.Pow(640320, 1.5))
'pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
pi = 1 / pi
'pi = 1/pi
The remainder of the console code just prints the result
If I use Decimal I get an exception at the deno = line (Value is too large or too small for a Decimal)
Sample run of the Python Code
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
How many iterations would you like?15
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303820
Press Enter
Actual Published Value of Pi
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819
Sample from VB ,NET code
3.1415926535897935600871733186068013310432434082031250000000000000 - this
3.1415926535897932384626433832795028841971693993751058209749445923 - known
Am I converting wrong or is it just that the decimal type in python is more precise and handles larger and smaller values than the .NET decimal / Double ?
Python "x ** y" is Math.Pow(x,y)
Python getcontext().prec=200 has no equivalent
Python for k in range: is "for k = 0 to n-1
Continue reading...