<commit_before>#!/usr/bin/python
import math

"""
**Find PI to the Nth Digit**
Enter a number and have the program generate PI up to
that many decimal places. Keep a limit to how far the program will go.
"""

def gregory_leibniz_series(n):
    assert n > 0

    pi = 1
    limit = 10000
    
    for i in range(2, limit, 1):
        pi += ((-1.0) ** (i + 1.0)) / (2.0 * i - 1.0)
    pi = 4 * pi
    print """
        Gregory-Leibniz Series
        pi = 4 (1 - (1/3) + (1/5) - (1/7) + ... (-1)^(i+1) / (2i -1))

        Leibniz's formula converges extremely slowly and is fairly inaccurate
        Calculating pi to 10 correct decimal places using direct summation
        of the series requires about 5,000,000,000 terms
        """        
    return pi    

def nilakantha_series(n):
    assert n > 0

def chudnovsky_algorithm(n):
    assert n > 0

def main():
    print "1) Gregory-Leibniz Series\n"
    print "2) Nilakantha Series\n"
    print "3) Chudnovsky Algorithm\n"
    print "4) Built in math library\n"

    method = raw_input("Which method would you like to use? ")
    try:
        method = int(method)
    except ValueError:
        print "Invalid input.  Exiting..."
        exit(0)

    n = raw_input("How many numbers do you need? ")
    try:
        n = int(n)
    except ValueError:
        print "Invalid input.  Exiting..."
        exit(0)
    if n > 0:
        if method == 1:
            print "Calculating pi using the Gregory-Leibniz series..."
            pi = gregory_leibniz_series(n)
        elif method == 2:
            print "Calculating pi using the Nilakantha series..."
        elif method == 3:
            print "Calculating pi using the Chudnovsky algorithm..."
        elif method == 4:
            print "Calculating pi using the built in math library..."
            pi = math.pi
        else:
            print "Invalid option.  Exiting..."
            exit(0)
        print '%.*f' % (n, pi)
    else:
        print "You must enter a number larger than 0.  Try again..."
        main()

if __name__ == "__main__":
    main()<commit_msg>Imported decimal library to increase precision.  Using floats and print only allowed for 67 digits<commit_after>#!/usr/bin/python
import math
from decimal import *

"""
**Find PI to the Nth Digit**
Enter a number and have the program generate PI up to
that many decimal places. Keep a limit to how far the program will go.
"""

def gregory_leibniz_series(n):
    assert n > 0

    getcontext().prec = n

    pi = Decimal(1)
    limit = 10000

    print
    print "Gregory-Leibniz Series\n"
    print "pi = 4 (1 - (1/3) + (1/5) - (1/7) + ... (-1)^(i+1) / (2i -1))\n"
    print "Leibniz's formula converges extremely slowly and is fairly inaccurate"
    print "Calculating pi to 10 correct decimal places using direct summation"
    print "of the series requires about 5,000,000,000 terms\n"

    print "Calculating pi to %d decimal places using the Gregory-Leibniz series. . . \n" % n
    
    for i in range(2, limit, 1):
        #pi += ((-1.0) ** (i + 1.0)) / (2.0 * i - 1.0)
        pi += (Decimal(-1) ** (Decimal(i) + Decimal(1))) / (Decimal(2) * Decimal(i) - Decimal(1))

    pi = Decimal(4) * Decimal(pi)

    return pi

def nilakantha_series(n):
    assert n > 0

def chudnovsky_algorithm(n):
    assert n > 0

def main():
    print "1) Gregory-Leibniz Series\n"
    print "2) Nilakantha Series\n"
    print "3) Chudnovsky Algorithm\n"
    print "4) Built in math library\n"

    method = raw_input("Which method would you like to use? ")
    try:
        method = int(method)
    except ValueError:
        print "Invalid input.  Exiting..."
        exit(0)

    n = raw_input("How many numbers do you need? ")
    try:
        n = int(n)
    except ValueError:
        print "Invalid input.  Exiting..."
        exit(0)
    if n > 0:
        getcontext().prec = n
        if method == 1:
            pi = gregory_leibniz_series(n)
        elif method == 2:
            print "Calculating pi using the Nilakantha series..."
        elif method == 3:
            print "Calculating pi using the Chudnovsky algorithm..."
        elif method == 4:
            print "Calculating pi using the built in math library..."
            pi = math.pi
        else:
            print "Invalid option.  Exiting..."
            exit(0)
        print pi
    else:
        print "You must enter a number larger than 0.  Try again..."
        main()

if __name__ == "__main__":
    main()
{'fraction_non_alphanumeric': 0.11554857527484855, 'fraction_numerical': 0.026923939869867623, 'mean_word_length': 2.022372881355932, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1591582', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1401, 'n_words': 516}