Compute pi to base 12 using Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dick Moores

    Compute pi to base 12 using Python?

    I need to figure out how to compute pi to base 12, to as many digits as
    possible. I found this reference,
    <http://mathworld.wolfr am.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@rcblue.com

  • Bengt Richter

    #2
    Re: Compute pi to base 12 using Python?

    On Wed, 13 Apr 2005 02:06:11 -0700, Dick Moores <rdm@rcblue.com > wrote:
    [color=blue]
    >I need to figure out how to compute pi to base 12, to as many digits as
    >possible. I found this reference,
    ><http://mathworld.wolfr am.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@rcblue.c om
    >[/color]
    See if this is enough digits for homework? ;-)

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

    3184809493B9186 64573A6211BB151 551A05729290A78 09A492742140A60 A55256A0661A037 53A3AA548056468 80181A
    3683083272BBBA0 A370B12265529A8 28903B4B256B840 3759A71626B8A54 687621849B849A8 225616B442796A3 1737B2
    29B239148985394 3B8763725616447 236B027A421AA17 A38B52A18A838B0 1514A51144A2331 5A3009A8906B61B 8B48A6
    2253A88A50A43BA 094457231593366 4476B3AABB77583 975120683526B75 B462060BB03B432 551913772729A21 475535
    31793848A0402B9 99B505853537446 5A6880671664403 9539A8431935198 527B9399B112990 ABB0383B1076454 24577A
    51601B3624A88B7 A676A3992912121 A213887B9287394 6A61332242217AA 735411535774493 9112602BA4B8888 18A326
    9222B5284877478 39994AB223B65B8 762695422822669 BA00A586097842A 51750362073B5A7 68363B21BB1A97A 4A1944
    477493998049221 75A068A46739461 990A2065BB0A30B BAB7024A585B1A8 4428195489784A0 7A331A7B0A15745 65B373
    B05B03A5A80A13A B87857734679985 558A5373178A7B2 8271992A3894A57 76085083B9B238B 222054246288864 1A2BAB
    8B3083AB4965917 2A312B785186544 94A068662586A18 1835A64440B2970 A12281397589881 536720890580103 288144
    922384142876332 9617531239B9A65 740558401453439 0B587625606BB80 923795944B43757 A431B0395562829 78A6A4
    9590553490BA184 4947175637A9082 47B501277224644 41380A852B0847B 5813019BB70A676 63B426565434069 884476
    132193344BA55A2 128A03838974606 B851B2979321A40 8067225A5AA4B34 64A1A1747359533 3909AB912707965 5B3164
    B68B9B28A9B818A 220A025AB093420 3995B7A62A7AA73 9355340539BA318 2905B193905603A 43B660B9426A922 946971
    44A896A5B233935 8BB2B7294BB8963 5B071A635121136 0B820B1882AB843 3B54757B87A3732 84B1BA182A10326 476B36
    9A4A6365B58B801 8994BB152556765 475A704BB94B6B2 A39458971A8B905 12786B502940481 864432355291617 0B3ABB
    7363496427B088B 68725A685700406 17949289077B278 069A09B559324B8 A66828B40549B02 96065B230033059 2569A7
    B76B92BA1293585 B6A9B604567A090 1362856373B4B56 897946256B4172B 1B5047435136474 9A33996A81BA884 7347A8
    411B850B79A0301 8291672AA094565 6A159AA6AA0A845 531A592005B8A34 366B882257107B1 90969A846474836 A98007
    50778920BA79729 7A2791101B0685A 86BB704B9BAA17B 055293679843B35 215B0A8B1182B61 1953B080AA5431B 219907
    A8448A81B1A9493 245676B88013B47 033524085959415 862101421661955 324657060196744 8B470174B924489 244481
    7453865A4003B5A A7176451AAB9068 1A949786154AA04 0477382BA693710 41710B8728458A2 3979252B2542367 53A44A
    1900AA283536A22 764881252574386 8B410A567794663 359A6726A528678 3328135114789B7 645505B04784802 0A730A
    9557B206776AA56 A19682744107901 306B29008808619 866B4911A05264B 872A46B53763839 32699531B449195 640B62
    A63622830886247 A47B39571698612 39358041AA28133 3622AA15912B0A6 36047A489BB0726 282A78B96671B27 305A96
    52496B9B999011A 7BA36898891665B 1A6009058978850 A21B01A158A1473 B84A192B8672542 A2A705658199520 7A436A
    5B3BA2824637A31 12ABB5717646820 6A071200A327B32 164251481007865 02AA21236ABB354 99277670A126973 058340
    3B1922A48385600 7301983989159BB 688A58B60233980 6B63002A339A50B 0BA533B84827793 913081070A32595 A10180
    3A9A20234691B1A 0B623274B69B0B4 468819516946105 9543A252BB05208 720BA13118266A8 72B26B9B584959B 451795
    19534B221A335A2 BB6971B3276B3A6 3A5B791723109B1 76529BB90651584 279B7825712521B 8269800738B07A6 2B1454
    788441445122409 293716562569655 7A78799A8212661 3535A36B410309B 759976119777B89 5801074B9779B9B 513753
    8B2799951012273 399BB818B721967 957713B90947B2A 11A6A665848B22B 531726616515939 323229080B8AB57 4AA749
    4773AB411A57150 203067A11294483 3235A86153803A9 8689A0762B79835 413A46B347888A4 AAB259665694B93 129B62
    1391751430A98B0 02620718437A7B8 5B891179479651A A3410663715415B 55BA47AA59465AB 81567B7655780A8 038135
    85230122578485B 071A529692B19A6 537B28616A63556 816945380634A90 470354AAB303884 B7B09B2037B9540 5BA145
    704B19B14AA8028 810881AB6072441 194A875477836B3 7704B5199062319 A33637543740356 2A663B835B89195 7883AB

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

    Regards,
    Bengt Richter

    Comment

    • Dick Moores

      #3
      Re: Compute pi to base 12 using Python?

      Bengt Richter wrote at 03:19 4/13/2005:[color=blue]
      >On Wed, 13 Apr 2005 02:06:11 -0700, Dick Moores <rdm@rcblue.com > wrote:
      >[color=green]
      > >I need to figure out how to compute pi to base 12, to as many digits as
      > >possible. I found this reference,
      > ><http://mathworld.wolfr am.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@rcblue.c om
      > >[/color]
      >See if this is enough digits for homework? ;-)[/color]

      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.
      [color=blue]
      >Hint: Lambert Meertens. Tweak the algorithm you find ;-)[/color]

      Sorry. Your hint is beyond me.

      Dick Moores
      rdm@rcblue.com


      Comment

      • Roman Neuhauser

        #4
        Re: Compute pi to base 12 using Python?

        # rdm@rcblue.com / 2005-04-13 03:27:06 -0700:[color=blue]
        > 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.
        >[color=green]
        > >Hint: Lambert Meertens. Tweak the algorithm you find ;-)[/color]
        >
        > Sorry. Your hint is beyond me.[/color]

        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

        Comment

        • Dan Bishop

          #5
          Re: Compute pi to base 12 using Python?

          Dick Moores wrote:[color=blue]
          > I need to figure out how to compute pi to base 12, to as many digits[/color]
          as[color=blue]
          > possible. I found this reference,[/color]
          <http://mathworld.wolfr am.com/Base.html>,[color=blue]
          > but I really don't understand it well enough.[/color]

          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:
          [color=blue][color=green][color=darkred]
          >>> 0.1[/color][/color][/color]
          0.1000000000000 0001

          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.100110011001 100110011001100 110011001100110 011010×2^-4), which is
          exactly equivalent to decimal
          0.1000000000000 000055511151231 257827021181583 404541015625, 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)
          [color=blue]
          > Could someone show me how to do what I need?[/color]

          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 = "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ"
          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(D IGITS[last_digit])
          digits.append(D IGITS[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.

          Comment

          • Dick Moores

            #6
            Re: Compute pi to base 12 using Python?

            Dan Bishop wrote at 04:07 4/13/2005:[color=blue]
            >(3) A function for converting numbers to their base-12 representation.
            >
            >For integers, this can be done with:
            >
            >DIGITS = "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ"
            >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(D IGITS[last_digit])
            > digits.append(D IGITS[num])
            > if is_negative:
            > digits.append("-")
            > digits.reverse( )
            > return ''.join(digits)[/color]

            I see this works perfectly for integers. Thanks!
            [color=blue]
            >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.[/color]

            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
            rdm@rcblue.com


            Comment

            • Roy Smith

              #7
              Re: Compute pi to base 12 using Python?

              In article <1113390479.738 210.49530@z14g2 000cwz.googlegr oups.com>,
              "Dan Bishop" <danb_83@yahoo. com> wrote:
              [color=blue]
              > But there's no reason other than tradition why you should arrange them
              > into groups of 10.[/color]

              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.

              Comment

              • Dan Bishop

                #8
                Re: Compute pi to base 12 using Python?

                Dick Moores wrote:[color=blue]
                > Dan Bishop wrote at 04:07 4/13/2005:[/color]
                ....[color=blue][color=green]
                > >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.[/color]
                >
                > But I'm sorry, but I can't follow you. I do have the first 10000 or[/color]
                so[color=blue]
                > places of pi base 10 (<http://mathwithmrherte .com/pi_digits.htm>) ,[/color]
                but[color=blue]
                > could you show me what to do with, say, just 3.14159?[/color]

                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.

                Comment

                • Scott David Daniels

                  #9
                  Re: Compute pi to base 12 using Python?

                  Roy Smith wrote:[color=blue]
                  > In article <1113390479.738 210.49530@z14g2 000cwz.googlegr oups.com>,
                  > "Dan Bishop" <danb_83@yahoo. com> wrote:
                  >
                  >[color=green]
                  >>But there's no reason other than tradition why you should arrange them
                  >>into groups of 10.[/color]
                  >
                  >
                  > 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.[/color]

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

                  --Scott David Daniels
                  Scott.Daniels@A cm.Org

                  Comment

                  • Roy Smith

                    #10
                    Re: Compute pi to base 12 using Python?

                    Scott David Daniels <Scott.Daniels@ Acm.Org> wrote:[color=blue]
                    >If you think those are fun, try base (1j - 1)[/color]

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

                    Comment

                    • Kristian Zoerhoff

                      #11
                      Re: Compute pi to base 12 using Python?

                      On 13 Apr 2005 12:06:26 -0400, Roy Smith <roy@panix.co m> wrote:[color=blue]
                      > Scott David Daniels <Scott.Daniels@ Acm.Org> wrote:[color=green]
                      > >If you think those are fun, try base (1j - 1)[/color]
                      >
                      > Get real. I can't imagine using anything so complex.[/color]

                      +1 QOTW

                      --
                      Kristian

                      kristian.zoerho ff(AT)gmail.com
                      zoerhoff(AT)fre eshell.org

                      Comment

                      • Dan Bishop

                        #12
                        Re: Compute pi to base 12 using Python?

                        Scott David Daniels wrote:[color=blue]
                        > Roy Smith wrote:[color=green]
                        > > In article <1113390479.738 210.49530@z14g2 000cwz.googlegr oups.com>,
                        > > "Dan Bishop" <danb_83@yahoo. com> wrote:
                        > >[color=darkred]
                        > >>But there's no reason other than tradition why you should arrange[/color][/color][/color]
                        them[color=blue][color=green][color=darkred]
                        > >>into groups of 10.[/color]
                        > >
                        > > Well, it is traditional for people to have 10 fingers :-)
                        > >
                        > > Other fun things to think about are negative bases. For example,[/color][/color]
                        3(10) =[color=blue][color=green]
                        > > 111(-2). That's 1*(-2)^2 + 1*(-2)^1 + 1*(-2)^0 = 4 - 2 + 1. I[/color][/color]
                        can't[color=blue][color=green]
                        > > think of any use for negative bases, but they are a fun game to[/color][/color]
                        play with[color=blue][color=green]
                        > > (if you're into that sort of stuff).
                        > >
                        > > Non-integer bases are fun too.[/color][/color]

                        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.
                        [color=blue]
                        > If you think those are fun, try base (1j - 1)[/color]

                        I think Knuth wrote something about complex bases back in the year
                        100020000000100 0000010001.

                        Comment

                        • Grant Edwards

                          #13
                          Re: Compute pi to base 12 using Python?

                          On 2005-04-13, Dan Bishop <danb_83@yahoo. com> wrote:
                          [color=blue]
                          > 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.[/color]

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

                          Comment

                          • Bengt Richter

                            #14
                            Re: Compute pi to base 12 using Python?

                            On Wed, 13 Apr 2005 03:27:06 -0700, Dick Moores <rdm@rcblue.com > wrote:
                            [color=blue]
                            >Bengt Richter wrote at 03:19 4/13/2005:[color=green]
                            >>On Wed, 13 Apr 2005 02:06:11 -0700, Dick Moores <rdm@rcblue.com > wrote:
                            >>[color=darkred]
                            >> >I need to figure out how to compute pi to base 12, to as many digits as
                            >> >possible. I found this reference,
                            >> ><http://mathworld.wolfr am.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@rcblue.c om
                            >> >[/color]
                            >>See if this is enough digits for homework? ;-)[/color]
                            >
                            >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.
                            >[color=green]
                            >>Hint: Lambert Meertens. Tweak the algorithm you find ;-)[/color]
                            >
                            >Sorry. Your hint is beyond me.
                            >[/color]
                            If you google with this line in the slot:

                            lambert meertens pi site:python.org

                            the first hit is



                            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.writ e(`int(d)`)
                            sys.stdout.flus h()

                            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.writ e(`int(d)`)
                            with
                            sys.stdout.writ e('%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 '0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ'[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

                            Comment

                            • mensanator@aol.com

                              #15
                              Re: Compute pi to base 12 using Python?

                              Dan Bishop wrote:[color=blue]
                              > Dick Moores wrote:[color=green]
                              > > I need to figure out how to compute pi to base 12, to as many[/color][/color]
                              digits[color=blue]
                              > as[color=green]
                              > > possible. I found this reference,[/color]
                              > <http://mathworld.wolfr am.com/Base.html>,[color=green]
                              > > but I really don't understand it well enough.[/color]
                              >
                              > How many stars are in "************** ***********"?
                              >
                              > You probably answered "25". This means that, for convenience, you've
                              > broken down the row of stars into ********** + ********** + *****,[/color]
                              that[color=blue]
                              > 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[/color]
                              them[color=blue]
                              > 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[/color]
                              represents[color=blue]
                              > eights.
                              >
                              > In general, in the base-r numeral system, the nth digit to the left[/color]
                              of[color=blue]
                              > 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,[/color]
                              8,[color=blue]
                              > 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"[/color]
                              the[color=blue]
                              > unique binary representation for decimal 25.
                              >
                              > Note that for bases larger than 10, the digits will be numbers that[/color]
                              are[color=blue]
                              > 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[/color]
                              right[color=blue]
                              > of the radix point (i.e., the "decimal point", but that term is
                              > inaccurate for bases other than ten) represents the value[/color]
                              radix**(-n).[color=blue]
                              >
                              > 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:
                              >[color=green][color=darkred]
                              > >>> 0.1[/color][/color]
                              > 0.1000000000000 0001
                              >
                              > 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.100110011001 100110011001100 110011001100110 011010×2^-4), which is
                              > exactly equivalent to decimal
                              > 0.1000000000000 000055511151231 257827021181583 404541015625, 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[/color]
                              believe[color=blue]
                              > that base-12 is superior to base-10.
                              > (http://www.dozenalsociety.org.uk)
                              >[color=green]
                              > > Could someone show me how to do what I need?[/color]
                              >
                              > 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.[/color]

                              That one's way too slow. I found the one I use below on Mathworld.
                              [color=blue]
                              >
                              > (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[/color]
                              base-10[color=blue]
                              > internal representation will introduce slight inaccuracies.[/color]

                              I'm using GMPY (see code).
                              [color=blue]
                              >
                              > (3) A function for converting numbers to their base-12[/color]
                              representation.

                              GMPY can do this also.
                              [color=blue]
                              >
                              > For integers, this can be done with:
                              >
                              > DIGITS = "0123456789ABCD EFGHIJKLMNOPQRS TUVWXYZ"
                              > 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(D IGITS[last_digit])
                              > digits.append(D IGITS[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.[/color]


                              # 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.nume r())
                              # seperately convert the numerator
                              den = gmpy.mpf(p.deno m())
                              # 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.nume r())
                              # meanwhile, convert this iteration
                              den = gmpy.mpf(p.deno m())
                              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.[color=blue][color=green][color=darkred]
                              >>> pialso(100,10)[/color][/color][/color]
                              314159265358979 323846264338327 950288419716939 937510582097494 459230781640628 620899862803482 5342117067
                              327

                              The first 100 digits of pi in base 12.[color=blue][color=green][color=darkred]
                              >>> pialso(100,12)[/color][/color][/color]
                              3184809493b9186 64573a6211bb151 551a05729290a78 09a492742140a60 a55256a0661a037 53a3aa548056468 80181a3682
                              353

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

                              """

                              Comment

                              Working...