Re: on the way to find pi!

2005-01-24 Thread Ali Polatel
when we change the code that way the programme gets awful slow when I want to calculate say 100 digits or more .Can't we just get the numbers out of there without changing the code radically thus not making the programme sloww?
		Do you Yahoo!? 
Yahoo! Search presents - Jib Jab's 'Second Term'-- 
http://mail.python.org/mailman/listinfo/python-list

on the way to find pi!

2005-01-23 Thread Ali Polatel

dear friends ,
I found acode 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 digitsdef 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!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- 
http://mail.python.org/mailman/listinfo/python-list

Re: on the way to find pi!

2005-01-23 Thread Daniel Bickett
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type help, copyright, credits or license for more information.
 import math
 math.pi
3.1415926535897931

Daniel Bickett
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: on the way to find pi!

2005-01-23 Thread Ali Polatel
that's just little near to pi... pi is so far away ;)__Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- 
http://mail.python.org/mailman/listinfo/python-list

Re: on the way to find pi!

2005-01-23 Thread Ali Polatel
write the code type str(pi(5)) and see what I mean__Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- 
http://mail.python.org/mailman/listinfo/python-list

Re: on the way to find pi!

2005-01-23 Thread Fredrik Lundh
Ali Polatel wrote:

 write the code type str(pi(5)) and see what I mean

it helps if you post the code you want help with in your first post,
so people don't have to guess.

the pi function doesn't return anything, it prints the value to stdout
(that's what the stdout.write things are doing).

if a function doesn't return anything, Python automagically inserts
a return None, which explains the None you're seeing (well, it
prints 3.1415None after your modification, so you probably did
something else to get 3.1415'None').

if you want the digits, you have to change the stdout.write calls to
something else.  say:

def pi(n=-1):
output = 
printed_decimal = False
r = f((1,0,1,1))
while n != 0:
if len(r) == 5:
output += str(r[4])
if not printed_decimal:
output += '.'
printed_decimal = True
n -= 1
r = f(r[:4])
return output

(this returns a string.  to convert it to a Python float, use float(pi(x)).  if 
x is
large enough, that gives you the same thing as math.pi).

/F 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [python-win32] on the way to find pi!

2005-01-23 Thread Michel Claveau
Hi ! 


Very more simplistic, but easy for to code : 

def pi(nb):
cpi=0
for i in xrange(1,nb,4):
cpi=cpi+4./i-4./(i+2.)
return(cpi)

print pi(99)



@-salutations
-- 
Michel Claveau


-- 
http://mail.python.org/mailman/listinfo/python-list