[Python] Pi Calculation Efficiency [closed]

Discussão em 'Python' iniciado por Stack, Dezembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    Is There any possible way to improve my code without using any non-python libraries. (8 GB of ram on a chromebook with 8 GB of vram)

    Note: I understand that the constant printing is taking a toll but its the least of my concerns


    # Bailey–Borwein–Plouffe formula

    from decimal import Decimal, getcontext, MAX_PREC

    getcontext().prec = MAX_PREC

    def split_even(item, split_num):
    return [item[i:i+split_num] for i in range(0, len(item), split_num)]

    def updateScreen(d_percent, percent):
    percent += d_percent
    print(f'{percent}% Complete.')
    return percent

    def pi(a):
    x = Decimal(0)
    d_percent = round(100/(a*6), len(str(a)))
    percent = 0 - d_percent
    for k in range(0, a):
    term = Decimal(4) / (8 * k + 1)
    percent = updateScreen(d_percent, percent)
    term -= Decimal(2) / (8 * k + 4)
    percent = updateScreen(d_percent, percent)
    term -= Decimal(1) / (8 * k + 5)
    percent = updateScreen(d_percent, percent)
    term -= Decimal(1) / (8 * k + 6)
    percent = updateScreen(d_percent, percent)
    x += term / (16 ** k)
    percent = updateScreen(d_percent, percent)
    print(f'Term {k} Complete.')
    return x


    pi = pi(int(input()))
    pi_list = split_even(str(pi), 1000)
    for i in pi_list:
    print(i)

    Continue reading...

Compartilhe esta Página