code optimization (calc PI) / New Algorithme for PI
Michael M.
michael at mustun.ch
Thu Jan 4 16:41:56 CET 2007
Mainly, it was fload-div. Changed to int-div (python //) runs faster.
Yes, this "gmpy" sounds good for calc things like that.
But not available on my machine.
ImportError: No module named gmpy
Anyway, thanks for posting. This gmpy module can be very intersting.
But right now, the focus was, if it's possible to translate a strange C
code into Python. And it is. Sure ;-)
Maybe, someone can also translate a very simple algorithm for calc a
range of PI in Python. (Available Code for C.)
http://crd.lbl.gov/~dhbailey/
http://crd.lbl.gov/~dhbailey/bbp-codes/piqpr8.c
But seems to be removed? It was working last days. (Maybe removed from
server.)
An other well known is: (but someone implemented this already in Python,
slow, and, depends on the previouse calc values, like most other
algorithmes.)
pi 01 01 01 01 01 01 01 01 01 01
__ = __ - __ + __ - __ + __-__+__-__+__-__
04 01 03 05 07 09 11 13 15 17 19 ...
pi/4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 ...
Or things like that:
http://numbers.computation.free.fr/Constants/PiProgram/pifast.html
> There is more than one way to skin this cat. A better algorithm helps
> a lot.
>
> If I really wanted lots of digits of pi from python I'd do it like
> this. This is an example of the right tool for the job. 0.5ms looks
> pretty good to me!
>
> >>> import math, gmpy
> >>> bits = int(800/math.log10(2))
> >>> start = time(); pi=gmpy.pi(bits); dt = time()-start
> >>> print "That took",dt,"seconds"
> That took 0.00055193901062 seconds
> >>> pi
> mpf('3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455e0',2657)
> >>>
>
More information about the Python-list
mailing list