Compute pi to base 12 using Python?

Discussion in 'Python' started by Dick Moores, Apr 13, 2005.

  1. Dick Moores

    Dick Moores Guest

    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
    Dick Moores, Apr 13, 2005
    #1
    1. Advertising

  2. On Wed, 13 Apr 2005 02:06:11 -0700, Dick Moores <> 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
    >
    >

    See if this is enough digits for homework? ;-)

    (Digits A and B are 10 and 11 in decimal respectively)

    3184809493B918664573A6211BB151551A05729290A7809A492742140A60A55256A0661A03753A3AA54805646880181A
    3683083272BBBA0A370B12265529A828903B4B256B8403759A71626B8A54687621849B849A8225616B442796A31737B2
    29B2391489853943B8763725616447236B027A421AA17A38B52A18A838B01514A51144A23315A3009A8906B61B8B48A6
    2253A88A50A43BA0944572315933664476B3AABB77583975120683526B75B462060BB03B432551913772729A21475535
    31793848A0402B999B5058535374465A68806716644039539A8431935198527B9399B112990ABB0383B107645424577A
    51601B3624A88B7A676A3992912121A213887B92873946A61332242217AA7354115357744939112602BA4B888818A326
    9222B528487747839994AB223B65B8762695422822669BA00A586097842A51750362073B5A768363B21BB1A97A4A1944
    47749399804922175A068A46739461990A2065BB0A30BBAB7024A585B1A84428195489784A07A331A7B0A1574565B373
    B05B03A5A80A13AB87857734679985558A5373178A7B28271992A3894A5776085083B9B238B2220542462888641A2BAB
    8B3083AB49659172A312B78518654494A068662586A181835A64440B2970A12281397589881536720890580103288144
    9223841428763329617531239B9A657405584014534390B587625606BB80923795944B43757A431B039556282978A6A4
    9590553490BA1844947175637A908247B50127722464441380A852B0847B5813019BB70A67663B426565434069884476
    132193344BA55A2128A03838974606B851B2979321A408067225A5AA4B3464A1A17473595333909AB9127079655B3164
    B68B9B28A9B818A220A025AB0934203995B7A62A7AA739355340539BA3182905B193905603A43B660B9426A922946971
    44A896A5B2339358BB2B7294BB89635B071A6351211360B820B1882AB8433B54757B87A373284B1BA182A10326476B36
    9A4A6365B58B8018994BB152556765475A704BB94B6B2A39458971A8B90512786B5029404818644323552916170B3ABB
    7363496427B088B68725A68570040617949289077B278069A09B559324B8A66828B40549B0296065B2300330592569A7
    B76B92BA1293585B6A9B604567A0901362856373B4B56897946256B4172B1B50474351364749A33996A81BA8847347A8
    411B850B79A03018291672AA0945656A159AA6AA0A845531A592005B8A34366B882257107B190969A846474836A98007
    50778920BA797297A2791101B0685A86BB704B9BAA17B055293679843B35215B0A8B1182B611953B080AA5431B219907
    A8448A81B1A9493245676B88013B470335240859594158621014216619553246570601967448B470174B924489244481
    7453865A4003B5AA7176451AAB90681A949786154AA040477382BA69371041710B8728458A23979252B254236753A44A
    1900AA283536A227648812525743868B410A567794663359A6726A5286783328135114789B7645505B047848020A730A
    9557B206776AA56A19682744107901306B29008808619866B4911A05264B872A46B5376383932699531B449195640B62
    A63622830886247A47B3957169861239358041AA281333622AA15912B0A636047A489BB0726282A78B96671B27305A96
    52496B9B999011A7BA36898891665B1A6009058978850A21B01A158A1473B84A192B8672542A2A7056581995207A436A
    5B3BA2824637A3112ABB57176468206A071200A327B3216425148100786502AA21236ABB35499277670A126973058340
    3B1922A483856007301983989159BB688A58B602339806B63002A339A50B0BA533B84827793913081070A32595A10180
    3A9A20234691B1A0B623274B69B0B44688195169461059543A252BB05208720BA13118266A872B26B9B584959B451795
    19534B221A335A2BB6971B3276B3A63A5B791723109B176529BB90651584279B7825712521B8269800738B07A62B1454
    7884414451224092937165625696557A78799A82126613535A36B410309B759976119777B895801074B9779B9B513753
    8B2799951012273399BB818B721967957713B90947B2A11A6A665848B22B531726616515939323229080B8AB574AA749
    4773AB411A57150203067A112944833235A86153803A98689A0762B79835413A46B347888A4AAB259665694B93129B62
    1391751430A98B002620718437A7B85B891179479651AA3410663715415B55BA47AA59465AB81567B7655780A8038135
    85230122578485B071A529692B19A6537B28616A63556816945380634A90470354AAB303884B7B09B2037B95405BA145
    704B19B14AA8028810881AB6072441194A875477836B37704B5199062319A336375437403562A663B835B891957883AB

    Hint: Lambert Meertens. Tweak the algorithm you find ;-)

    Regards,
    Bengt Richter
    Bengt Richter, Apr 13, 2005
    #2
    1. Advertising

  3. Dick Moores

    Dick Moores Guest

    Bengt Richter wrote at 03:19 4/13/2005:
    >On Wed, 13 Apr 2005 02:06:11 -0700, Dick Moores <> 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
    > >
    > >

    >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.

    Dick Moores
    Dick Moores, Apr 13, 2005
    #3
  4. # / 2005-04-13 03:27:06 -0700:
    > Bengt Richter wrote at 03:19 4/13/2005:
    > 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.


    it says "use google".

    --
    How many Vietnam vets does it take to screw in a light bulb?
    You don't know, man. You don't KNOW.
    Cause you weren't THERE. http://bash.org/?255991
    Roman Neuhauser, Apr 13, 2005
    #4
  5. Dick Moores

    Dan Bishop Guest

    Dick Moores 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.


    How many stars are in "*************************"?

    You probably answered "25". This means that, for convenience, you've
    broken down the row of stars into ********** + ********** + *****, that
    is, 2 tens with 5 left over, which the base-10 numeral system denotes
    as "25".

    But there's no reason other than tradition why you should arrange them
    into groups of 10. For example, you could write it as ******** +
    ******** + ******** + *, or 3 eights plus 1. In octal (base-8)
    notation, this is written as "31"; the "tens" place in octal represents
    eights.

    In general, in the base-r numeral system, the nth digit to the left of
    the ones digit represents r^n. For example, in the binary number
    11001, the place values for each digit are, right to left, 1, 2, 4, 8,
    and 16, so the number as a whole represents
    1×16+1×8+0×4+0×2+1×1=16+8+1=25. This analogous to 25=2×10+5 in
    base-10.

    It's also possible to write it as 3×8+0×4+0×2+1×1 = 3001 base 2,
    but by convention, base-r only uses the digits in range(r). This
    ensures a unique represenation for each number. This makes "11001" the
    unique binary representation for decimal 25.

    Note that for bases larger than 10, the digits will be numbers that are
    not single digits in base 10. By convention, letters are used for
    larger digits: A=10, B=11, C=12, ... Z=35. For example, the number
    (dec) 2005 = 1×12³+1×12²+11×12+1×1 is represented in base-12 by
    "11B1".

    Fractions are handled in a similar manner. The nth place to the right
    of the radix point (i.e., the "decimal point", but that term is
    inaccurate for bases other than ten) represents the value radix**(-n).

    For example, in binary,
    0.1 = 1/2 = dec. 0.5
    0.01 = 1/4 = dec. 0.25
    0.11 = 1/2 + 1/4 = 3/4 = dec. 0.75
    0.001 = 1/8 = dec. 0.125
    0.01010101... = 1/4 + 1/16 + 1/64 + ... = 1/3
    0.0001100110011... = 1/10 = dec. 0.1

    The last row explains why Python gives:

    >>> 0.1

    0.10000000000000001

    Most computers store floating-point numbers in binary, which doesn't
    have a finite representation for one-tenth. The above result is
    rounded to 53 signficant bits
    (1.100110011001100110011001100110011001100110011010×2^-4), which is
    exactly equivalent to decimal
    0.1000000000000000055511151231257827021181583404541015625, but gets
    rounded to 17 significant digits for display.

    Similarly, in base-12:

    0.1 = 1/12
    0.14 = 1/12 + 4/144 = 1/9
    0.16 = 1/12 + 6/144 = 1/8
    0.2 = 2/12 = 1/6
    0.3 = 3/12 = 1/4
    0.4 = 4/12 = 1/3
    0.6 = 6/12 = 1/2
    0.8 = 8/12 = 2/3
    0.9 = 9/12 = 3/4
    0.A = 10/12 = 5/6

    Notice that several common fractions have simpler representations in
    base-12 than in base-10. For this reason, there are people who believe
    that base-12 is superior to base-10.
    (http://www.dozenalsociety.org.uk)

    > Could someone show me how to do what I need?


    You'll need 3 things:

    (1) An algorithm for computing approximations of pi.

    The simplest one is 4*(1-1/3+1/5-1/7+1/9-1/11+...), which is based on
    the Taylor series expansion of 4*arctan(1).

    There are other, faster ways. Search Google for them.

    (2) An unlimited-precision numeric representation. The standard
    "float" isn't good enough: It has only 53 bits of precision, which is
    only enough for 14 base-12 digits.

    The "decimal" module will probably work, although of course its base-10
    internal representation will introduce slight inaccuracies.

    (3) A function for converting numbers to their base-12 representation.

    For integers, this can be done with:

    DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    def itoa(num, radix=10):
    is_negative = False
    if num < 0:
    is_negative = True
    num = -num
    digits = []
    while num >= radix:
    num, last_digit = divmod(num, radix)
    digits.append(DIGITS[last_digit])
    digits.append(DIGITS[num])
    if is_negative:
    digits.append("-")
    digits.reverse()
    return ''.join(digits)

    For a floating-point number x, the representation with d "decimal"
    places count be found by taking the representation of int(round(x *
    radix ** d)) and inserting a "." d places from the right.
    Dan Bishop, Apr 13, 2005
    #5
  6. Dick Moores

    Dick Moores Guest

    Dan Bishop wrote at 04:07 4/13/2005:
    >(3) A function for converting numbers to their base-12 representation.
    >
    >For integers, this can be done with:
    >
    >DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    >def itoa(num, radix=10):
    > is_negative = False
    > if num < 0:
    > is_negative = True
    > num = -num
    > digits = []
    > while num >= radix:
    > num, last_digit = divmod(num, radix)
    > digits.append(DIGITS[last_digit])
    > digits.append(DIGITS[num])
    > if is_negative:
    > digits.append("-")
    > digits.reverse()
    > return ''.join(digits)


    I see this works perfectly for integers. Thanks!

    >For a floating-point number x, the representation with d "decimal"
    >places count be found by taking the representation of int(round(x *
    >radix ** d)) and inserting a "." d places from the right.


    But I'm sorry, but I can't follow you. I do have the first 10000 or so
    places of pi base 10 (<http://mathwithmrherte.com/pi_digits.htm>), but
    could you show me what to do with, say, just 3.14159?

    I apologize for being so dense.

    Dick Moores
    Dick Moores, Apr 13, 2005
    #6
  7. Dick Moores

    Roy Smith Guest

    In article <>,
    "Dan Bishop" <> wrote:

    > But there's no reason other than tradition why you should arrange them
    > into groups of 10.


    Well, it is traditional for people to have 10 fingers :)

    Other fun things to think about are negative bases. For example, 3(10) =
    111(-2). That's 1*(-2)^2 + 1*(-2)^1 + 1*(-2)^0 = 4 - 2 + 1. I can't
    think of any use for negative bases, but they are a fun game to play with
    (if you're into that sort of stuff).

    Non-integer bases are fun too.
    Roy Smith, Apr 13, 2005
    #7
  8. Dick Moores

    Dan Bishop Guest

    Dick Moores wrote:
    > Dan Bishop wrote at 04:07 4/13/2005:

    ....
    > >For a floating-point number x, the representation with d "decimal"
    > >places count be found by taking the representation of int(round(x *
    > >radix ** d)) and inserting a "." d places from the right.

    >
    > But I'm sorry, but I can't follow you. I do have the first 10000 or

    so
    > places of pi base 10 (<http://mathwithmrherte.com/pi_digits.htm>),

    but
    > could you show me what to do with, say, just 3.14159?


    First, decide how many "decimal" places to use for the conversion.
    Five decimal digits is equivalent to 5*log(10)/log(12) = 4.63 base-12
    digits, so use 4 digits.

    Next, multiply by 12**4, obtaining the value 65144.01024, and round to
    the nearest integer, 65144. Convert this to base 12, obtaining 31848.
    But this is 12**4 times the number we really want, so divide this by
    12**4 (i.e., shift the radix point left 4 places), for a final result
    of 3.1848.
    Dan Bishop, Apr 13, 2005
    #8
  9. Roy Smith wrote:
    > In article <>,
    > "Dan Bishop" <> wrote:
    >
    >
    >>But there's no reason other than tradition why you should arrange them
    >>into groups of 10.

    >
    >
    > Well, it is traditional for people to have 10 fingers :)
    >
    > Other fun things to think about are negative bases. For example, 3(10) =
    > 111(-2). That's 1*(-2)^2 + 1*(-2)^1 + 1*(-2)^0 = 4 - 2 + 1. I can't
    > think of any use for negative bases, but they are a fun game to play with
    > (if you're into that sort of stuff).
    >
    > Non-integer bases are fun too.


    If you think those are fun, try base (1j - 1)

    --Scott David Daniels
    Scott David Daniels, Apr 13, 2005
    #9
  10. Dick Moores

    Roy Smith Guest

    Scott David Daniels <> wrote:
    >If you think those are fun, try base (1j - 1)


    Get real. I can't imagine using anything so complex.
    Roy Smith, Apr 13, 2005
    #10
  11. On 13 Apr 2005 12:06:26 -0400, Roy Smith <> wrote:
    > Scott David Daniels <> wrote:
    > >If you think those are fun, try base (1j - 1)

    >
    > Get real. I can't imagine using anything so complex.


    +1 QOTW

    --
    Kristian

    kristian.zoerhoff(AT)gmail.com
    zoerhoff(AT)freeshell.org
    Kristian Zoerhoff, Apr 13, 2005
    #11
  12. Dick Moores

    Dan Bishop Guest

    Scott David Daniels wrote:
    > Roy Smith wrote:
    > > In article <>,
    > > "Dan Bishop" <> wrote:
    > >
    > >>But there's no reason other than tradition why you should arrange

    them
    > >>into groups of 10.

    > >
    > > Well, it is traditional for people to have 10 fingers :)
    > >
    > > Other fun things to think about are negative bases. For example,

    3(10) =
    > > 111(-2). That's 1*(-2)^2 + 1*(-2)^1 + 1*(-2)^0 = 4 - 2 + 1. I

    can't
    > > think of any use for negative bases, but they are a fun game to

    play with
    > > (if you're into that sort of stuff).
    > >
    > > Non-integer bases are fun too.


    Pi has an interesting representation in bases between 0 and 1,
    exclusive. There are a finite number of digits after the radix point,
    but an infinite number _before_ it.

    > If you think those are fun, try base (1j - 1)


    I think Knuth wrote something about complex bases back in the year
    1000200000001000000010001.
    Dan Bishop, Apr 13, 2005
    #12
  13. On 2005-04-13, Dan Bishop <> wrote:

    > Pi has an interesting representation in bases between 0 and 1,
    > exclusive. There are a finite number of digits after the radix point,
    > but an infinite number _before_ it.


    You really oughtn't make me think so hard right after lunch.

    --
    Grant Edwards grante Yow! I'd like some JUNK
    at FOOD... and then I want to
    visi.com be ALONE --
    Grant Edwards, Apr 13, 2005
    #13
  14. On Wed, 13 Apr 2005 03:27:06 -0700, Dick Moores <> wrote:

    >Bengt Richter wrote at 03:19 4/13/2005:
    >>On Wed, 13 Apr 2005 02:06:11 -0700, Dick Moores <> 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
    >> >
    >> >

    >>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
    Bengt Richter, Apr 13, 2005
    #14
  15. Dick Moores

    Guest

    Dan Bishop wrote:
    > Dick Moores 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.

    >
    > How many stars are in "*************************"?
    >
    > You probably answered "25". This means that, for convenience, you've
    > broken down the row of stars into ********** + ********** + *****,

    that
    > is, 2 tens with 5 left over, which the base-10 numeral system denotes
    > as "25".
    >
    > But there's no reason other than tradition why you should arrange

    them
    > into groups of 10. For example, you could write it as ******** +
    > ******** + ******** + *, or 3 eights plus 1. In octal (base-8)
    > notation, this is written as "31"; the "tens" place in octal

    represents
    > eights.
    >
    > In general, in the base-r numeral system, the nth digit to the left

    of
    > the ones digit represents r^n. For example, in the binary number
    > 11001, the place values for each digit are, right to left, 1, 2, 4,

    8,
    > and 16, so the number as a whole represents
    > 1×16+1×8+0×4+0×2+1×1=16+8+1=25. This analogous to 25=2×10+5 in
    > base-10.
    >
    > It's also possible to write it as 3×8+0×4+0×2+1×1 = 3001 base 2,
    > but by convention, base-r only uses the digits in range(r). This
    > ensures a unique represenation for each number. This makes "11001"

    the
    > unique binary representation for decimal 25.
    >
    > Note that for bases larger than 10, the digits will be numbers that

    are
    > not single digits in base 10. By convention, letters are used for
    > larger digits: A=10, B=11, C=12, ... Z=35. For example, the number
    > (dec) 2005 = 1×12³+1×12²+11×12+1×1 is represented in base-12 by
    > "11B1".
    >
    > Fractions are handled in a similar manner. The nth place to the

    right
    > of the radix point (i.e., the "decimal point", but that term is
    > inaccurate for bases other than ten) represents the value

    radix**(-n).
    >
    > For example, in binary,
    > 0.1 = 1/2 = dec. 0.5
    > 0.01 = 1/4 = dec. 0.25
    > 0.11 = 1/2 + 1/4 = 3/4 = dec. 0.75
    > 0.001 = 1/8 = dec. 0.125
    > 0.01010101... = 1/4 + 1/16 + 1/64 + ... = 1/3
    > 0.0001100110011... = 1/10 = dec. 0.1
    >
    > The last row explains why Python gives:
    >
    > >>> 0.1

    > 0.10000000000000001
    >
    > Most computers store floating-point numbers in binary, which doesn't
    > have a finite representation for one-tenth. The above result is
    > rounded to 53 signficant bits
    > (1.100110011001100110011001100110011001100110011010×2^-4), which is
    > exactly equivalent to decimal
    > 0.1000000000000000055511151231257827021181583404541015625, but gets
    > rounded to 17 significant digits for display.
    >
    > Similarly, in base-12:
    >
    > 0.1 = 1/12
    > 0.14 = 1/12 + 4/144 = 1/9
    > 0.16 = 1/12 + 6/144 = 1/8
    > 0.2 = 2/12 = 1/6
    > 0.3 = 3/12 = 1/4
    > 0.4 = 4/12 = 1/3
    > 0.6 = 6/12 = 1/2
    > 0.8 = 8/12 = 2/3
    > 0.9 = 9/12 = 3/4
    > 0.A = 10/12 = 5/6
    >
    > Notice that several common fractions have simpler representations in
    > base-12 than in base-10. For this reason, there are people who

    believe
    > that base-12 is superior to base-10.
    > (http://www.dozenalsociety.org.uk)
    >
    > > Could someone show me how to do what I need?

    >
    > You'll need 3 things:
    >
    > (1) An algorithm for computing approximations of pi.
    >
    > The simplest one is 4*(1-1/3+1/5-1/7+1/9-1/11+...), which is based on
    > the Taylor series expansion of 4*arctan(1).
    >
    > There are other, faster ways. Search Google for them.


    That one's way too slow. I found the one I use below on Mathworld.

    >
    > (2) An unlimited-precision numeric representation. The standard
    > "float" isn't good enough: It has only 53 bits of precision, which is
    > only enough for 14 base-12 digits.
    >
    > The "decimal" module will probably work, although of course its

    base-10
    > internal representation will introduce slight inaccuracies.


    I'm using GMPY (see code).

    >
    > (3) A function for converting numbers to their base-12

    representation.

    GMPY can do this also.

    >
    > For integers, this can be done with:
    >
    > DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    > def itoa(num, radix=10):
    > is_negative = False
    > if num < 0:
    > is_negative = True
    > num = -num
    > digits = []
    > while num >= radix:
    > num, last_digit = divmod(num, radix)
    > digits.append(DIGITS[last_digit])
    > digits.append(DIGITS[num])
    > if is_negative:
    > digits.append("-")
    > digits.reverse()
    > return ''.join(digits)
    >
    > For a floating-point number x, the representation with d "decimal"
    > places count be found by taking the representation of int(round(x *
    > radix ** d)) and inserting a "." d places from the right.



    # better pi/2 = 1 + 1/3 + (1*2)/(3*5) + (1*2*3)/(3*5*7) + ...

    import gmpy
    # unlimited precision math library

    def pialso(n,b):
    # input number of digits (n) in requested base (b)
    p = gmpy.mpq(1,1)
    # gmpy rationals are unlimited precision
    sn = 1
    sd = 3
    c = gmpy.mpq(sn,sd)
    # create the next term to be summed
    num = gmpy.mpf(p.numer())
    # seperately convert the numerator
    den = gmpy.mpf(p.denom())
    # and denominator to gmpy floats
    f = (num/den) * 2
    # to get unlimited precision float
    olds = gmpy.fdigits(f,b,n,0,1,2)
    # extract the requested digits and do base conversion
    done = 0
    while done==0:
    p = p + c
    # sum next term of pi equation
    sn += 1
    # set numerator
    sd += 2
    # and demoniator
    c = c * gmpy.mpq(sn,sd)
    # to create term for next iteration
    num = gmpy.mpf(p.numer())
    # meanwhile, convert this iteration
    den = gmpy.mpf(p.denom())
    f = (num/den) * 2
    # to an unlimited precision float
    s = gmpy.fdigits(f,b,n,0,1,2)
    if s[0]==olds[0]:
    # we're done when the number of digits
    done = 1
    # we want stops changing
    else:
    # otherwise, keep iterating until we reach the
    olds = s
    # the desired convergence
    print s[0],sn
    # prints the digits and how many iterations it took
    print

    """

    The first 100 digits of pi in base 10.
    >>> pialso(100,10)

    3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
    327

    The first 100 digits of pi in base 12.
    >>> pialso(100,12)

    3184809493b918664573a6211bb151551a05729290a7809a492742140a60a55256a0661a03753a3aa54805646880181a3682
    353

    Note it took more iterations (longer to converge) because base 12
    digits are "bigger" than base 10.

    """
    , Apr 13, 2005
    #15
  16. Dick Moores

    Dan Guest

    On 13 Apr 2005 12:06:26 -0400, (Roy Smith) wrote:

    >Scott David Daniels <> wrote:
    >>If you think those are fun, try base (1j - 1)

    >
    >Get real. I can't imagine using anything so complex.


    Well said. :)

    Dan
    Dan, Apr 14, 2005
    #16
  17. Dick Moores

    Dan Guest

    On Wed, 13 Apr 2005 03:27:06 -0700, Dick Moores <>
    wrote:

    > I'm just trying to help an artist acquaintance who needs (I just
    >learned) the first 3003 digits of pi to the base 12.


    Now you've got me curious. Why would an artist want the first 3003
    digits of pi to the base 12?

    Dan
    Dan, Apr 14, 2005
    #17
  18. Dick Moores

    John Machin Guest

    On Wed, 13 Apr 2005 08:28:29 -0400, Roy Smith <> wrote:

    >In article <>,
    > "Dan Bishop" <> wrote:
    >
    >> But there's no reason other than tradition why you should arrange them
    >> into groups of 10.

    >
    >Well, it is traditional for people to have 10 fingers :)


    According to anthropology archives, there was once a tribe called
    "OS/360 system programmers" who would cut off their thumbs and great
    toes in order that they might better count in hexadecimal.
    John Machin, Apr 14, 2005
    #18
  19. Dick Moores

    Dick Moores Guest

    Dan wrote at 18:02 4/13/2005:
    >On Wed, 13 Apr 2005 03:27:06 -0700, Dick Moores <>
    >wrote:
    >
    > > I'm just trying to help an artist acquaintance who needs (I just
    > >learned) the first 3003 digits of pi to the base 12.

    >
    >Now you've got me curious. Why would an artist want the first 3003
    >digits of pi to the base 12?


    He says,
    Do you know how I can get "base12 pi"?
    Because the chromatic scale is base12.
    c c# d d# e f f# g g# a a# b

    Dick
    Dick Moores, Apr 14, 2005
    #19
  20. Dick Moores

    Paul Rubin Guest

    Dick Moores <> writes:
    > 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?


    Using the GNU "bc" utility:

    $ bc -l
    bc 1.06
    Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
    This is free software with ABSOLUTELY NO WARRANTY.
    For details type `warranty'.
    scale = 3000 # number of output places wanted
    obase = 12 # output base
    print 4 * a(1) # pi = 4*arctan(1)
    3.184809493B918664573A6211BB151551A05729290A7809A492742140A60A55256A\
    0661A03753A3AA54805646880181A3683083272BBBA0A370B12265529A828903B4B2\
    56B8403759A71626B8A54687621849B849A8225616B442796A31737B229B23914898\
    53943B8763725616447236B027A421AA17A38B52A18A838B01514A51144A23315A30\
    09A8906B61B8B48A62253A88A50A43BA0944572315933664476B3AABB77583975120\
    683526B75B462060BB03B432551913772729A2147553531793848A0402B999B50585\
    35374465A68806716644039539A8431935198527B9399B112990ABB0383B10764542\
    4577A51601B3624A88B7A676A3992912121A213887B92873946A61332242217AA735\
    4115357744939112602BA4B888818A3269222B528487747839994AB223B65B876269\
    5422822669BA00A586097842A51750362073B5A768363B21BB1A97A4A19444774939\
    9804922175A068A46739461990A2065BB0A30BBAB7024A585B1A84428195489784A0\
    7A331A7B0A1574565B373B05B03A5A80A13AB87857734679985558A5373178A7B282\
    71992A3894A5776085083B9B238B2220542462888641A2BAB8B3083AB49659172A31\
    2B78518654494A068662586A181835A64440B2970A12281397589881536720890580\
    1032881449223841428763329617531239B9A657405584014534390B587625606BB8\
    0923795944B43757A431B039556282978A6A49590553490BA1844947175637A90824\
    7B50127722464441380A852B0847B5813019BB70A67663B426565434069884476132\
    193344BA55A2128A03838974606B851B2979321A408067225A5AA4B3464A1A174735\
    95333909AB9127079655B3164B68B9B28A9B818A220A025AB0934203995B7A62A7AA\
    739355340539BA3182905B193905603A43B660B9426A92294697144A896A5B233935\
    8BB2B7294BB89635B071A6351211360B820B1882AB8433B54757B87A373284B1BA18\
    2A10326476B369A4A6365B58B8018994BB152556765475A704BB94B6B2A39458971A\
    8B90512786B5029404818644323552916170B3ABB7363496427B088B68725A685700\
    40617949289077B278069A09B559324B8A66828B40549B0296065B2300330592569A\
    7B76B92BA1293585B6A9B604567A0901362856373B4B56897946256B4172B1B50474\
    351364749A33996A81BA8847347A8411B850B79A03018291672AA0945656A159AA6A\
    A0A845531A592005B8A34366B882257107B190969A846474836A9800750778920BA7\
    97297A2791101B0685A86BB704B9BAA17B055293679843B35215B0A8B1182B611953\
    B080AA5431B219907A8448A81B1A9493245676B88013B47033524085959415862101\
    4216619553246570601967448B470174B9244892444817453865A4003B5AA7176451\
    AAB90681A949786154AA040477382BA69371041710B8728458A23979252B25423675\
    3A44A1900AA283536A227648812525743868B410A567794663359A6726A528678332\
    8135114789B7645505B047848020A730A9557B206776AA56A19682744107901306B2\
    9008808619866B4911A05264B872A46B5376383932699531B449195640B62A636228\
    30886247A47B3957169861239358041AA281333622AA15912B0A636047A489BB0726\
    282A78B96671B27305A9652496B9B999011A7BA36898891665B1A6009058978850A2\
    1B01A158A1473B84A192B8672542A2A7056581995207A436A5B3BA2824637A3112AB\
    B57176468206A071200A327B3216425148100786502AA21236ABB35499277670A126\
    9730583403B1922A483856007301983989159BB688A58B602339806B63002A339A50\
    B0BA533B84827793913081070A32595A101803A9A20234691B1A0B623274B69B0B44\
    688195169461059543A252BB05208720BA13118266A872B26B9B584959B44B
    quit
    $

    The arctan calculation takes about 20 sec on an Athlon of around 2 ghz.
    Paul Rubin, Apr 14, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2tpcmtieQ==?=

    Problem using COMPUTE with SPROC on ASP Application

    =?Utf-8?B?c2tpcmtieQ==?=, Nov 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    295
    Scott Allen
    Nov 1, 2004
  2. Dick Moores

    Re: Compute pi to base 12 using Python?

    Dick Moores, Apr 14, 2005, in forum: Python
    Replies:
    2
    Views:
    315
    Dick Moores
    Apr 17, 2005
  3. Dick Moores

    Re: Compute pi to base 12 using Python?

    Dick Moores, Apr 15, 2005, in forum: Python
    Replies:
    2
    Views:
    445
    Tim Roberts
    Apr 17, 2005
  4. Rahul
    Replies:
    1
    Views:
    521
  5. PerlFAQ Server
    Replies:
    0
    Views:
    264
    PerlFAQ Server
    Feb 2, 2011
Loading...

Share This Page