Hi there,

> According to this:
> 
> http://news.bbc.co.uk/1/hi/technology/8442255.stm
> 
> someone has just computed pi to 2.7 trillion digits on a "desktop
> computer".  The article does not mention software.
> 
> How well would Sage do?

I once tried to find the shortest program which compute pi in sage, with the
requirement that your are not allowed to use any precomputed value (except
perhaps a number of iterations for the required precision). I came up with the
following incredibly simple program which use only additions and memory except
a multiplication and a division at the end. Moreover the convergence is
reasonably fast (close to one decimal every other iterations). Note that the
following code is meant to be as short as possible, it could be easily
optimized:

sage: nLoop = 60
sage: l = [1]
sage: for n in range(2, nLoop):
...     ll = [sum(l[:i]) for i in range(n-1, -1, -1)]
...     l = ll
...
sage: RealField(100)(2*ll[0]*len(ll)/sum(ll))
3.1415926535897932384626433830

I'd be interested if someone knows a short pythonic way to write the line

    ll = [sum(l[:i]) for i in range(n-1, -1, -1)]

without recomputing the sums. The following is too long for my taste:

    ll = l[:]
    ll.append(0)
    for i in range(len(l)):
        ll[-(i+2)] = ll[-(i+1)] + l[i]

The perhaps challenging question if you don't know is why does this works ?
Why does this converge this fast ? ;-)

Cheers,

Florent
-- 
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to