Python

Copy Code

1 from decimal import Decimal, getcontext 2 import math 3 def calc(n): 4 t = Decimal(0) 5 pi = Decimal(0) 6 deno = Decimal(0) 7 k = 0 8 for k in range(n): 9 t = (Decimal(-1)**k)*(math.factorial(6*k))*(13591409+545140134*k) 10 deno = math.factorial(3*k)*(math.factorial(k)**Decimal(3))*(640320**(3*k)) 11 pi += Decimal(t)/Decimal(deno) 12 pi = pi * Decimal(12)/Decimal(640320**Decimal(1.5)) 13 pi = 1/pi 14 return str(pi) 15 print(calc(1))

It looks like some python implementations do implicit type conversions while other do not.

Sorry that I can't give more trustfull information.

Ok, after copy/paste I need to adjust two line break, but that is simply a formatting issue.

[Edit]

I formatted a liitle bit your code and now I can copy/paste it to the link above and it runs without any additional modifications.

Traceback (most recent call last):

File "C:\python\picalc.py", line 16, in <module>

print(calc(1))

File "C:\python\picalc.py", line 10, in calc

t = (Decimal(-1)**k)*(math.factorial(Decimal(6)*k))*(13591409+545140134*k)

TypeError: 'decimal.Decimal' object cannot be interpreted as an integer

>>>

~~but I guess~~

`Decimal(13591409+545140134*k)`

and

`Decimal(640320**(3*k))`

maybe helps.

[Edit]

Here Repl.it - Python Online Compiler and IDE - Fast, Powerful, Free[^] I get same error like you.

After removing Decimal conversion here

`t = (Decimal(-1)**k)*(math.factorial(6*k))*(13591409+545140134*k)`

instead of

`t = (Decimal(-1)**k)*(math.factorial(Decimal(6)*k))*(13591409+545140134*k)`

it runs