-1

I want to caculate pi to a big digit in python 2, but the code doesn't work - it just closes and I want to see how long it takes.

from decimal import Decimal, getcontext
from time import time, strftime
import datetime

def arccot(x, digits):
    getcontext().prec = digits
    total = 0
    n = 1  
    while Decimal((2 * n - 1) * x ** (2 * n - 1)) < Decimal(10 ** digits):
        term = ((-1)**(n - 1)) * 1 / Decimal((2 * n - 1) * x ** (2 * n - 1))
        total += term
        n += 1
    return total

def pi(decimals):
    timestart = time()
    print "pi = " + str(Decimal(4 * (4 * arccot(5, decimals + 3) - arccot(239,
        decimals + 3))).quantize(Decimal(10) ** (-decimals)))
    timeelapsedint = round(time() - timestart, 2)
    timeelapsedstr = str(datetime.timedelta(seconds = round(
        timeelapsedint, 0)))
    print "runtime: " + timeelapsedstr + " or " + str(
        timeelapsedint) + " seconds."
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
coder
  • 143
  • 1
  • 11

1 Answers1

2

Just call your function (notice the last line):

# Pi Calculator

# Python 2.7.3

# After running, type "pi(n)" where n is the number of decimals for pi.  For

#  example, if you would like to calculate 100 decimals for pi, type "pi(100)".



# import python libraries

from decimal import Decimal, getcontext

from time import time, strftime

import datetime

# arccot function using power formula arccot = 1/x - 1/(3x^3) + 1/(5x^5) ...

def arccot(x, digits):

    # set precision and starting values

    getcontext().prec = digits

    total = 0
    n = 1

    # loop while new term is large enough to actually change the total

    while Decimal((2 * n - 1) * x ** (2 * n - 1)) < Decimal(10 ** digits):

        # find value of new term

        term = ((-1)**(n - 1)) * 1 / Decimal((2 * n - 1) * x ** (2 * n - 1))

        # add the new term to the total

        total += term

        # next n

        n += 1

    # return the sum

    return total



# pi function

def pi(decimals):

    # start timer

    timestart = time()

    # find pi using Machin's Formula pi = 4 * (4 * arccot(5) - arccot(239))

    #  and the power formula for arccot (see arccot function above)
    print "pi = " + str(Decimal(4 * (4 * arccot(5, decimals + 3) - arccot(239, decimals + 3))).quantize(Decimal(10) ** (-decimals)))



    # display elapsed time

    timeelapsedint = round(time() - timestart, 2)

    timeelapsedstr = str(datetime.timedelta(seconds = round(timeelapsedint, 0)))

    print "runtime: " + timeelapsedstr + " or " + str(timeelapsedint) + " seconds."

pi(1000)

Outputs:

pi = 3.14159265358979323....more digits
runtime: 0:00:00 or 0.22 seconds.

Also, your code is not properly formatted, but I can't edit it. My snippet here is formatted properly.

Jeppe
  • 1,830
  • 3
  • 24
  • 33
  • it still dont output anything but thanks – coder Feb 07 '19 at 17:41
  • @coder Did you add the call to `pi(1000)` after the two functions? How are you running it? – Jeppe Feb 07 '19 at 17:42
  • Well technically speaking, if you run it as is, it won't do anything as you're not printing the value – Peter Feb 07 '19 at 17:43
  • i run it by opening it with python 2.7.3 and i copied all the code you wrote and pasting it in the file – coder Feb 07 '19 at 17:44
  • how do you print the value – coder Feb 07 '19 at 17:45
  • @Peter What do you mean? The function `pi` doesn't return a value - it calculates it and prints it. – Jeppe Feb 07 '19 at 17:46
  • @Jeppe My bad haha, I just saw "not outputting anything" combined with not printing the function result, and assumed it was that without actually reading the code :p – Peter Feb 08 '19 at 01:28