[Python] Python - trying to calculate digits of pi and cannot get past 48 digits after the...

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to write a program to calclulate digits of pi using the Nilakantha Series in python. Every time it runs though it will not give me more than 50 decimals. Still learning python so any help is appreciated.

    # Program using Nilakantha Series to crunch digits of pi
    from math import *
    from decimal import *

    getcontext().prec = 200 # this is not doing anything

    # epsilon is how accurate I want to be to pi
    EPSILON = 0.000000000000000000000000000000000000000000000000000001

    sum = float(3)
    step = 0

    i = 2

    while abs(pi - sum) >= EPSILON:
    step += 1
    print (step)
    if step % 2 == 1:
    sum += 4.0 / (i * (i + 1) * (i + 2))
    i += 2
    else:
    sum -= 4.0 / (i * (i + 1) * (i + 2))
    i += 2

    print (Decimal(sum))
    print (Decimal(pi))
    print ("Total itterations: ", step)
    print ("Accurate to: ", EPSILON)

    Continue reading...

Compartilhe esta Página