Pi in Programming Languages

Posted by Mariia Mykhailova on March 14, 2011

March 14th is a worldwide Pi (π) day. Founded in 1989 and officially recognized in 2009, this day can be celebrated in multiple ways, the most popular of which are eating circular pies and discussing things related to Pi. It would be weird not to have a couple of programming languages based on Pi or at least named after it. In this article I'll talk about them - after all, one has to support good traditions :-)

1. Pi - dialect of Brainfuck

Brainfuck has plenty of dialects for all occasions, including Pi day. The commands of the Pi language are the same as in Brainfuck, but written as erroneous digits of Pi number in the following way. For each command a position is chosen in which it will be hidden (the positions are ordered in the same way as in the original code). A table of commands and corresponding digits is constructed from the initial one

<   >   +   —   .   ,   [   ]
0   1   2   3   4   5   6   7   8

by shifting the row of commands one position to the right, starting with the command which is above the digit in this position. Thus, if the chosen position is second after decimal point, with digit 4 in it, the table will look like this:

<   >   +   —       .   ,   [   ]
0   1   2   3   4   5   6   7   8

After this the command is encoded with a digit below it, which replaces the correct digit in the chosen position. The resulting Pi program will be a number similar to Pi, but somewhat different from it. Thus, program "Hello, World!" in Brainfuck would look like this:

++++++[>++++++++++++<-]>.>++++++++++[>++++++++++<-]>+.+++++++..+++.>++++[>++++++
+++++<-]>.<+++[>----<-]>.<<<<<+++[>+++++<-]>.>>.+++.------.--------.>>+.

and would be written in Pi like this:

3.141592653589793238462623382272502824197169299275107820904924592337816406386238
99262833482534311206728234808621328230264709314460935058223872535941812844111745
00841022019385311055296426229289549302819244388109726652334471204756422337867231
65221231909345628566933460342610454226248213391607264249148273720587036656315582
17288153092396282225439171532436789559536003133023024882044652108412695192151163
94330573703656595909530921261173839326137921051125420742623799227495273538857227
24892227938133011749109833675362442656243086321294946795024737130702479860343702
77453921711629317375838467480846766440513202056822724526351082178577132275778260
91736271767204684409312229532301462492853110307922896892089235450199501120290219
65862034218129813624774731309964518707241349993993372978039951049734732816036348
59504445345544690330263252250825304468003522193158817101

2. Another Pi Language

Weird even for esoteric languages, this one exists only as a theoretical description. The idea is based on a common theory that you can find any information in the Pi number, as long as you choose correct offset and encoding. In this case Pi is written in binary:

11.
00100100 00111111 01101010 10001000 10000101 10100011 00001000 11010011 
00010011 00011001 10001010 00101110 00000011 01110000 01110011 01000100 
10100100 00001001 00111000 00100010 00101001 10011111 00110001 11010000 
00001000 00101110 11111010 10011000 11101100 01001110 01101100 10001001 
...

The source code of a program in Another Pi Language is a pair of numbers: the offset of the first bit to be read and the number of bytes to read. The bytes are converted into characters with corresponding ASCII-codes, and the resulting text is interpreted as source code in another language (this language should be given along with the pair of numbers).

Theoretically one can find any sequence of bits of any length in Pi notation, and thus the code of any arbitrarily complicated program. This language will be Turing-complete (since it's capable of anything that is doable in any other language). It's another story that coding in it is about as pleasant as in Malbolge. However, unlike Malbolge, Another Pi Language allows to code "Hello, World!" easily:

3 1 HQ9+

To interpret this code,

  • read 1 byte, starting from 3rd notation bit. First 1 is bit number 0, so the first bit to be read will be the second zero after decimal point: 01001000;
  • convert the result into decimal (72) and into a character with this ASCII-code (H);
  • interpret the resulting character as source code of a program in HQ9+ - for this cute language H is the actual command to print "Hello, World!". How convenient.

Several more examples of varying length:

  • canonical example "99 bottles of beer"
    113 1 HQ9+ (9)
  • read one character and output it
    102168614(+-1) 2 Brainfuck (,.)
  • infinite loop
    3234901746(+-1) 3 Boolfuck (*[])

(two last examples were constructed using Search Pi)

3. Pattern language Pi

Something average between a language design tool and a philosophy of software design. Is based on the concept of semantic and syntactical extendability of programming languages using the means of languages themselves.

4. Bonus :-)

A little gift for Pi day - Pi Day Challenge is a yearly series of puzzles related to Pi.

Freelance Jobs

blog comments powered by Disqus