Compute pi to base 12 using Python?

I

Ivan Van Laningham

D

Dick Moores

Paul Rubin wrote at 18:20 4/13/2005:
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)

I don't believe GNU "bc" is available for Windows, is it?

Thanks,

Dick Moores
(e-mail address removed)
 
S

Steve Holden

Dick said:
Dan wrote at 18:02 4/13/2005:



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
So it's, like, the guy is going to "play pi"? So why does the melody
have 3003 notes?

regards
Steve
 
D

Doug Schwarz

Dick Moores said:
Dan wrote at 18:02 4/13/2005:

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

Does your artist friend have any idea what base 12 means?

The chromatic scale is based on one twelfth powers of two, i.e., if the
frequency of a note in the scale is f(n), then the frequency of the next
note is given by

f(n+1) = f(n) * 2^(1/12)

so by the time you go all 12 notes in an octave you have doubled the
frequency. There is nothing here involving base 12 or pi.
 
D

Dick Moores

Doug Schwarz wrote at 20:14 4/13/2005:
Does your artist friend have any idea what base 12 means?

The chromatic scale is based on one twelfth powers of two, i.e., if the
frequency of a note in the scale is f(n), then the frequency of the next
note is given by

f(n+1) = f(n) * 2^(1/12)

so by the time you go all 12 notes in an octave you have doubled the
frequency. There is nothing here involving base 12 or pi.

He's a friend of a friend. I don't know what he knows, but I'll forward
this to MY friend. Thanks.

Dick
 
R

Roman Yakovenko

Hi. I would like to freeze python application on linux. There are a
few tools that make the job to be done:
freeze ( comes with python )
cx_Freeze
Gordon McMillan's installer


Is it possible to freeze python application on linux in such way that
it doesn't depends on python installed on cu
 
R

Roman Yakovenko

Sorry for previous post - hit the wrong button

Hi. I would like to freeze python application on linux. There are a
few tools that make the job to be done:
freeze ( comes with python )
cx_Freeze
Gordon McMillan's installer

I have one problem with all of them: they require python to be
installed on target machine.
May be I missed something or did not understand right the docs? Also
if I am right could you point me to "freeze" tool that doesn't require
python installed on customer computer?

For windows I have py2exe. What should I use for linux to get same affect?

Thanks

Roman
 
B

Bengt Richter

]
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`)
Typo, sorry ----------------^
that back-tick should not be there ;-/
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 would make this into a base-parameterized generator, something like
... base = long(base)
... k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
... while ndigits:
... # Next approximation
... p, q, k = k*k, 2L*k+1L, k+1L
... a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
... # Print common digits
... d, d1 = a/b, a1/b1
... while d == d1:
... yield int(d)
... if ndigits is not True:
... ndigits -= 1
... if ndigits == 0: break
... a, a1 = base*(a%b), base*(a1%b1)
... d, d1 = a/b, a1/b1
... ... assert base>=2 and base <= len(digchars) and ndigits>=0
... for d in pigen(base, ndigits):
... yield digchars[d]

[ I just realized the original had tabs, so the above is fixed and pasted after the below ]
... ...
314159265358979323846264338327950288419716939937510582097494>>> ...
... )
314159265358979323846264338327950288419716939937510582097494 ...
314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211
706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895
493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360
726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138
414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272
489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317
675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014
654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999
837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783
875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053
217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530
185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838
175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783
74494482553797747268471040475346462080466842590694912933136770289891521047521Traceback (most rec
ent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in pidigits
File "<stdin>", line 16, in pigen
KeyboardInterrupt

To make a file for your friend,

That didn't take very long. No line breaks though. You can do that quick and dirty with
>>> open('pi12by60.txt','w').write( ... ''.join(c+'\n'[:(i+1)%60==0] for i,c in enumerate(pidigits(12, 3003)))+'\n')
>>>

(adding the last EOL since we know 3003%60 =!0 ;-)

You could speed up the initial burst a lot, but after a while the data passing overhead is not
a significant part of the whole time.

Hm, ... that first digit still needs to be fixed for base <4 ;-)

Regards,
Bengt Richter
 
B

Bengt Richter

Does your artist friend have any idea what base 12 means?
Maybe he wants to play sounds of pi and has found base 10 digits don't
hit all the notes, and he may not be as dumb as you think ;-)
The chromatic scale is based on one twelfth powers of two, i.e., if the
frequency of a note in the scale is f(n), then the frequency of the next
note is given by

f(n+1) = f(n) * 2^(1/12)

so by the time you go all 12 notes in an octave you have doubled the
frequency. There is nothing here involving base 12 or pi.
I expect something interesting and imaginative ;-)
It might also be interesting to keep a running sum of the base 12 values
and use sum % 88 to select piano keys, to let it walk intervals outside
of a single octave ;-)

I found using different base digits to draw end-to-end vectors selected from
equal vectors equally spaced in all base directions for a "random" walk picture
was interesting. I also changed the color according to various criteria such
as digit run length ending on the current digit. Maybe the artist could scale
that up and put it on the wall as backdrop to the music ;-)
It would be easy to generate postscript for it, that could be scaled up and
printed wall size. Or pdf. I'm tempted to play with my pdf plotting toy and
maybe make it work ;-)

I'm kind of curious what the ear could pick up about pi from hearing the sequence
as notes. Or intervals, or grouped to make chords even. Anyone have an easy python
midi interface for windows to play on the sound card? I could generate a .wav file
to play tones, but midi would be much more compact ;-)

Regards,
Bengt Richter
 
B

Bengt Richter

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.
I suspect using four dates back to the nixie tribe, who practiced bi-quinary.
And they would cut off little toes and fingers rather, because then the thumb
was more significant and it was easier to remember it as 5 (or 0 if hidden)
with the rest counting octally ;-)

Regards,
Bengt Richter
 
B

Bengt Richter

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.

That's cool. I will have to find out about bc. Thanks ;-)

Interesting to note, it also took about 20 sec on my _old_ machine:
... t0 = clock()
... open('pi12by60.txt','w').write(
... ''.join(c+'\n'[:(i+1)%60==0] for i,c in enumerate(pidigits(12, 3003)))+'\n')
... print clock()-t0
... 22.3866400935

That's on a 300Mhz Pentium II using the Lambert Meertens algorithm for pi ;-)

Regards,
Bengt Richter
 
R

Richie Hindle

[Dan]
Now you've got me curious. Why would an artist want the first 3003
digits of pi to the base 12?
[Dick]
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

He should read Douglas Adams' fictional essay "Music and Fractal
Landscapes", from "Dirk Gently's Holistic Detective Agency":

"I believe that there must be a form of
music inherent in nature, in natural objects, in the patterns
of natural processes. A music that would be as deeply
satisfying as any naturally occurring beauty [...]"

You can see the text here:


http://66.102.9.104/search?q=cache:...s+adams"+"Music+and+Fractal+Landscapes"&hl=en

or via this tinyurl:

http://tinyurl.com/6ugnk

(Search within that page for the phrase "Music and Fractal Landscapes".
Or Google for it, which is how I found the link.)
 
G

Greg Lindstrom

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

It might feel more "natural" to do this with 'e' (2.718...)
--greg
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[Doug Schwarz]
The chromatic scale is based on one twelfth powers of two, i.e., if
the frequency of a note in the scale is f(n), then the frequency of
the next note is given by f(n+1) = f(n) * 2^(1/12)

This easy view of things has been known for a long time, but has only
been popular (relatively) recently. Traditionally, scale designers were
definitely running after rational proportions between scale notes, not
fearing some problems they necessarily create, because such scales are
often nicer and interesting to the musical ear.

I should relate this discussion to Python somehow! :) Easy, as I have a
few Python programs doing various scale computations -- I should try to
bundle these together somewhere in my personal Web site, some day...
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[Bengt Richter]
It might also be interesting to keep a running sum of the base 12
values and use sum % 88 to select piano keys, to let it walk intervals
outside of a single octave ;-)

The generated would then run from the low octaves to high octaves
monotically, then start over again and again.

Maybe a more interesting approach might be to pick the note in the same
octave, the octave below or above, where the new note is closest to the
preceding one.
a "random" walk picture was interesting.

Using the closest note would have similarity with a random walk, given
Ï€ digits are seemingly random. On a random walk, one gets away from
the departure point on average, the distance being proportional to
sqrt(N) where N is the number of steps. So, when using the closest
note, one would need a corrective device nevertheless so notes are kept
near the middle of the range of comfortable audible frequencies.
Anyone have an easy python midi interface for windows to play on the
sound card? I could generate a .wav file to play tones, but midi
would be much more compact ;-)

There are surely many. I use my own (Python) interfaces on Linux, and
even there, by combining a few tools, it is rather easy to get .WAV
files out of MIDI. In any case, googling around might help.
 
B

Bill Mill

I don't know. It probably works ok under Cygwin at least.

bc definitely works on cygwin, and is available at
http://gnuwin32.sourceforge.net/packages/bc.htm for windows. Make sure
you download both the dependencies and the binary package for it to
work. I put the dll from the dependancy archive in c:/winnt/system32
and it worked.

It should be noted that running the win32 bc from cygwin messed up my
terminal, so I recommend running it from a cmd window (which worked
fine).

Peace
Bill Mill
bill.mill at gmail.com
 
N

Nick Craig-Wood

I'm using GMPY (see code).
[snip]

If you are using gmpy you might as well do it like this.....

gmpy.pi() uses the Brent-Salamin Arithmetic-Geometric Mean formula for
pi IIRC. This converges quadratically, and it will calculate you a
million places without breaking a sweat.
'3.184809493b918664573a6211bb151551a05729290a7809a492742140a60a55256a0661a03753a3aa54805646880181a3683083272bbba0a370b12265529a828903b4b256b8403759a71626b8a54687621849b849a8225616b442796a31737b229b2391489853943b8763725616447236b027a421aa17a38b52a18a838b01514a51144a23315a3009a8906b61b8b48a62253a88a50a43ba0944572315933664476b3aabb77583975120683526b75b462060bb03b432551913772729a2147553531793848a0402b999b5058535374465a68806716644039539a8431935198527b9399b112990abb0383b107645424577a51601b3624a88b7a676a3992912121a213887b92873946a61332242217aa7354115357744939112602ba4b888818a3269222b528487747839994ab223b65b8762695422822669ba00a586097842a51750362073b5a768363b21bb1a97a4a194447749399804922175a068a46739461990a2065bb0a30bbab7024a585b1a84428195489784a07a331a7b0a1574565b373b05b03a5a80a13ab87857734679985558a5373178a7b28271992a3894a5776085083b9b238b2220542462888641a2bab8b3083ab49659172a312b78518654494a068662586a181835a64440b2970a122813975898815367208905801032881449223841428763329617531239b9a657405584014534390b587625606bb80923795944b43757a431b039556282978a6a49590553490ba1844947175637a908247b50127722464441380a852b0847b5813019bb70a67663b426565434069884476132193344ba55a2128a03838974606b851b2979321a408067225a5aa4b3464a1a17473595333909ab9127079655b3164b68b9b28a9b818a220a025ab0934203995b7a62a7aa739355340539ba3182905b193905603a43b660b9426a92294697144a896a5b2339358bb2b7294bb89635b071a6351211360b820b1882ab8433b54757b87a373284b1ba182a10326476b369a4a6365b58b8018994bb152556765475a704bb94b6b2a39458971a8b90512786b5029404818644323552916170b3abb7363496427b088b68725a68570040617949289077b278069a09b559324b8a66828b40549b0296065b2300330592569a7b76b92ba1293585b6a9b604567a0901362856373b4b56897946256b4172b1b50474351364749a33996a81ba8847347a8411b850b79a03018291672aa0945656a159aa6aa0a845531a592005b8a34366b882257107b190969a846474836a9800750778920ba797297a2791101b0685a86bb704b9baa17b055293679843b35215b0a8b1182b611953b080aa5431b219907a8448a81b1a9493245676b88013b470335240859594158621014216619553246570601967448b470174b9244892444817453865a4003b5aa7176451aab90681a949786154aa040477382ba69371041710b8728458a23979252b254236753a44a1900aa283536a227648812525743868b410a567794663359a6726a5286783328135114789b7645505b047848020a730a9557b206776aa56a19682744107901306b29008808619866b4911a05264b872a46b5376383932699531b449195640b62a63622830886247a47b3957169861239358041aa281333622aa15912b0a636047a489bb0726282a78b96671b27305a9652496b9b999011a7ba36898891665b1a6009058978850a21b01a158a1473b84a192b8672542a2a7056581995207a436a5b3ba2824637a3112abb57176468206a071200a327b3216425148100786502aa21236abb35499277670a1269730583403b1922a483856007301983989159bb688a58b602339806b63002a339a50b0ba533b84827793913081070a32595a101803a9a20234691b1a0b623274b69b0b44688195169461059543a252bb05208720ba13118266a872b26b9b584959b45179519534b221a335a2bb6971b3276b3a63a5b791723109b176529bb90651584279b7825712521b8269800738b07a62b14547884414451224092937165625696557a78799a82126613535a36b410309b759976119777b895801074b9779b9b5137538b2799951012273399bb818b722@0'
 
M

mensanator

Nick said:
I'm using GMPY (see code).
[snip]

If you are using gmpy you might as well do it like this.....

gmpy.pi() uses the Brent-Salamin Arithmetic-Geometric Mean formula for
pi IIRC. This converges quadratically, and it will calculate you a
million places without breaking a sweat.

It would be nice if that were documented. What do I have to do, go get
the documentation for the original GMP to find out what else is in GMPY
that they didn't include in the doc file?

'3.184809493b918664573a6211bb151551a05729290a7809a492742140a60a55256a0661a03753a3aa54805646880181a3683083272bbba0a370b12265529a828903b4b256b8403759a71626b8a54687621849b849a8225616b442796a31737b229b2391489853943b8763725616447236b027a421aa17a38b52a18a838b01514a51144a23315a3009a8906b61b8b48a62253a88a50a43ba0944572315933664476b3aabb77583975120683526b75b462060bb03b432551913772729a2147553531793848a0402b999b5058535374465a68806716644039539a8431935198527b9399b112990abb0383b107645424577a51601b3624a88b7a676a3992912121a213887b92873946a61332242217aa7354115357744939112602ba4b888818a3269222b528487747839994ab223b65b8762695422822669ba00a586097842a51750362073b5a768363b21bb1a97a4a194447749399804922175a068a46739461990a2065bb0a30bbab7024a585b1a84428195489784a07a331a7b0a1574565b373b05b03a5a80a13ab87857734679985558a5373178a7b28271992a3894a5776085083b9b238b2220542462888641a2bab8b3083ab49659172a312b78518654494a068662586a181835a64440b2970a122813975898815367208905801032881449223841428763329617531239b9a657405584014534390b587625606bb80923795944b43757a431b039556282978a6a49590553490ba1844947175637a908247b50127722464441380a852b0847b5813019bb70a67663b426565434069884476132193344ba55a2128a03838974606b851b2979321a408067225a5aa4b3464a1a17473595333909ab9127079655b3164b68b9b28a9b818a220a025ab0934203995b7a62a7aa739355340539ba3182905b193905603a43b660b9426a92294697144a896a5b2339358bb2b7294bb89635b071a6351211360b820b1882ab8433b54757b87a373284b1ba182a10326476b369a4a6365b58b8018994bb152556765475a704bb94b6b2a39458971a8b90512786b5029404818644323552916170b3abb7363496427b088b68725a68570040617949289077b278069a09b559324b8a66828b40549b0296065b2300330592569a7b76b92ba1293585b6a9b604567a0901362856373b4b56897946256b4172b1b50474351364749a33996a81ba8847347a8411b850b79a03018291672aa0945656a159aa6aa0a845531a592005b8a34366b882257107b190969a846474836a9800750778920ba797297a2791101b0685a86bb704b9baa17b055293679843b35215b0a8b1182b611953b080aa5431b219907a8448a81b1a9493245676b88013b470335240859594158621014216619553246570601967448b470174b9244892444817453865a4003b5aa7176451aab90681a949786154aa040477382ba69371041710b8728458a23979252b254236753a44a1900aa283536a227648812525743868b410a567794663359a6726a5286783328135114789b7645505b047848020a730a9557b206776aa56a19682744107901306b29008808619866b4911a05264b872a46b5376383932699531b449195640b62a63622830886247a47b3957169861239358041aa281333622aa15912b0a636047a489bb0726282a78b96671b27305a9652496b9b999011a7ba36898891665b1a6009058978850a21b01a158a1473b84a192b8672542a2a7056581995207a436a5b3ba2824637a3112abb57176468206a071200a327b3216425148100786502aa21236abb35499277670a1269730583403b1922a483856007301983989159bb688a58b602339806b63002a339a50b0ba533b84827793913081070a32595a101803a9a20234691b1a0b623274b69b0b44688195169461059543a252bb05208720ba13118266a872b26b9b584959b45179519534b221a335a2bb6971b3276b3a63a5b791723109b176529bb90651584279b7825712521b8269800738b07a62b14547884414451224092937165625696557a78799a82126613535a36b410309b759976119777b895801074b9779b9b5137538b2799951012273399bb818b722@0'
http://www.craig-wood.com/nick
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top