Compute pi to base 12 using Python?

Bengt Richter bokr at oz.net
Wed Apr 13 17:52:45 EDT 2005


On Wed, 13 Apr 2005 03:27:06 -0700, Dick Moores <rdm at rcblue.com> wrote:

>Bengt Richter wrote at 03:19 4/13/2005:
>>On Wed, 13 Apr 2005 02:06:11 -0700, Dick Moores <rdm at rcblue.com> wrote:
>>
>> >I need to figure out how to compute pi to base 12, to as many digits as
>> >possible. I found this reference,
>> ><http://mathworld.wolfram.com/Base.html>, but I really don't understand
>> >it well enough. Could someone show me how to do what I need?
>> >
>> >Thanks,
>> >
>> >Dick Moores
>> >rdm at rcblue.com
>> >
>>See if this is enough digits for homework? ;-)
>
>This is not homework, nor am I a student, though I am trying to learn 
>Python. I'm just trying to help an artist acquaintance who needs (I just 
>learned) the first 3003 digits of pi to the base 12.
>
>>Hint: Lambert Meertens. Tweak the algorithm you find ;-)
>
>Sorry. Your hint is beyond me.
>
If you google with this line in the slot:

    lambert meertens pi site:python.org

the first hit is

    http://mail.python.org/pipermail/tutor/2000-August/002143.html

In that (scroll down) you will find:

--------------------------------------------------
# Based on a algorithm of Lambert Meertens (remember those days of the
# B -> ABC-programming language!!!)


import sys

def main():
    k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
    while 1:
        p, q, k = k*k, 2L*k+1L, k+1L
        a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
        d, d1 = a/b, a1/b1
        while d == d1:
            output(d)
            a, a1 = 10L*(a%b), 10L*(a1%b1)
            d, d1 = a/b, a1/b1

def output(d):
    sys.stdout.write(`int(d)`)
    sys.stdout.flush()

main()

# Reading/writing Python source often gives me the impression of
# reading/writing a poem!
# Layout, indentation, rythm, I like the look and feel!

# What does this tiny program do? It is not a sonnet, even not a
# pi-sonnet, but it surely produces Pi!
--------------------------------------------------

If you replace
            a, a1 = 10L*(a%b), 10L*(a1%b1)
with
            a, a1 = 12L*(a%b), 12L*(a1%b1)

and
    sys.stdout.write(`int(d)`)
with
    sys.stdout.write('%X'%d`)

and run it, I think it will do what you want, even though I haven't worked through exactly
what it's doing, though it's pretty. (For confidence I just tried it and decoded the result
far enough to match math.pi exactly ;-)

(the %X formats hex, but for single digits that's fine for base 12, giving A for 10 and B for 11.
If you want bases >16 you'll have to use something like '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'[digitvalue])

BTW, I find that googling restricted to site:python.org
is a good bet for python-related info. After that, if no joy,
you can of course expand the search.

BTW2, I played with using pi digits to various bases as directions for turtle-style plotting, to
see if my eye would pick out patterns in the random-seeming sequence. Also played with coloring
the vector steps. I.e., set up a base-length (12 in your case) list of (dx,dy) tuples
for relative plot vectors and just relatively plot deltalist[pidigitvalue] and update the display
so you can see it develop. It was kind of interesting. For bases under 4 you have to decide what
to do with the "first" digit ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list