def leibniz_pi(n_terms):
    s = 0
    for n in range(n_terms+1):
        s += ((-1)**n)/(2*n+1)
    return 4*s
    
res = leibniz_pi(1000)
print("The approximation using terms up to n = 1000 is", res)

################################################################################

#Idea: try the largest possible divisor first
def simplify_fraction(n, m):
    '''Simplify the fraction n/m and print the result
    
    Arguments:
    n, m -- positive non-zero integers
    '''
    factor = min(n, m)
    
    while factor >= 2:
        if n % factor == 0 and m % factor == 0:
            n, m = n//factor, m//factor
            break  #factor is the largest common divisor, so no
                   #no need to proceed
        factor -= 1
    
    #Take care of the case where m == 1, so we don't need to 
    #display the whole fraction
    if m == 1:
        print(n)
    else:
        print(str(n) + "/" + str(m))
################################################################################            

def agree_to_n_sig_figs(x, y, n):
    '''Return True iff x and y agree to n significant figures
    
    Arguments:
    x, y -- floats between 1 and 9.99999999
    '''
    
    return int(x*10**(n-1)) == int(y*10**(n-1))
    
            
def approx_pi(n_sig_fig):
    s = 0
    i = 0
    
    while not agree_to_n_sig_figs(4*s, math.pi, n_sig_fig):
        s += ((-1)**i)/(2*i+1)
        i += 1
    return i
        
        
    