[Tutor] on the way to find pi

Ali Polatel alipolatel at yahoo.com
Sun Jan 23 19:25:27 CET 2005


dear friends ,
I found a code which calculates pi with an interesting algorithm the programme code is below:
from sys import stdout
def f((q,r,t,k)):
    n = (3*q+r) / t
    if (4*q+r) / t == n:
        return (10*q,10*(r-n*t),t,k,n)
    else:
        return (q*k, q*(4*k+2)+r*(2*k+1),t*(2*k+1),k+1)
# Call pi(20) for first 20 digits, or pi() for all digits
def pi(n=-1):
    printed_decimal = False
    r = f((1,0,1,1))
    while n != 0:
        if len(r) == 5:
            stdout.write(str(r[4]))
            if not printed_decimal:
                stdout.write('.')
                printed_decimal = True
            n -= 1
        r = f(r[:4])
    #stdout.write('\n')
if __name__ == '__main__':
    from sys import argv
    try:
        digit_count = long(argv[1])
    except:
        digit_count=int(raw_input('How many digits? :'))
        pi(digit_count)
        
This code gives the number in an unusual format like "3.1415'None'" it has a number part and a string part . I want to seperate these from easc other but I couldn't manage. I mean when I try to turn it into string format then try to use things like [:4] or like that they don't work.Any idea how to seperate this 'None' from the number and make it a real normal number on which I can do operations like +1 -1 or like that :)
Regards

		
---------------------------------
Do you Yahoo!?
 The all-new My Yahoo! – What will yours do?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050123/e378f7d8/attachment.htm


More information about the Tutor mailing list