Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2008-11-05 21:15:09

All_Is_Number
Member
Registered: 2006-07-10
Posts: 258

Has anyone ever played around with …

… brainf***? It is an unfortunately (albeit accurately) named Turing complete language with only eight single character commands:

.  ,  <  >  [  ]  +  -

The language assumes (at least) a 30,000 element array, initialized to zero, and a moveable pointer, which is always pointing at a single element of the array.

>  moves pointer forward one position, relative to current position

< moves pointer back one position, relative to current position

+  increments value of array element in pointer's current position one unit

-  decrements value of array element in pointer's current position one unit

[  begins a "while value of element at current pointer position ≠ 0" loop

]  represents the end of the loop structure

. outputs ascii character associated with the value of element in current pointer position

, accepts ascii value of a single character as input and assigns that value to element at current position


Any character other than these eight, including spaces and new lines, is a comment.

brainf*** (<- not capitalized) is a Turing complete programming language. If a problem has a solution that can be calculated, given sufficient memory and time (and/or processing power), any Turing complete language , including brainf***, can be used to do the calculations. Compilers smaller than 200 bytes have been written for the language.

Here is an uncommented version of Hello World! in brainf***, ending in a new line (from Wikipedia):

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

A couple years ago, I wrote a brainf*** program that printed out the first 43 (I think) numbers of the Fibonacci sequence, without reusing anyone else's code. Due to hardware or compiler limitations (can't recall which) anything beyond that would have been inaccurately calculated. I'll see if I can't dig up the code.

Is anyone up to the challenge of writing a program in brainf***? Post them here. smile

Last edited by All_Is_Number (2008-11-05 21:15:46)


You can shear a sheep many times but skin him only once.

Offline

#2 2008-11-05 23:09:56

MathsIsFun
Administrator
Registered: 2005-01-21
Posts: 7,713

Re: Has anyone ever played around with …

I read about it a few years ago, and was impressed by it's illegibility dizzy (...however I can see that "..+++." prints "llo", or at least some double letter followed by a letter three positions ahead!)

But it is impressive that it can do so much with so little, and so illustrates the fundamentals of programming. Anything else is just to make life easier!


"The physicists defer only to mathematicians, and the mathematicians defer only to God ..."  - Leon M. Lederman

Offline

#3 2009-01-02 09:18:34

random_fruit
Member
Registered: 2008-12-25
Posts: 39

Re: Has anyone ever played around with …

The reason why brainf*ck is such a nasty programming language is that it doesn't use assembler style nmemonics.  A very long time ago I learnt a language called "CESIL" which is documented on the web.  See http://www.obelisk.demon.co.uk/cesil/  The power of the languages is similar, but the programmer-hostile form of the programs means it is really hard to read.

Computer science has a similar beast named "Turing Machine" and a theory (theorm?) which says that if a program can be written it can be written for a Turing Machine.  Look here: http://en.wikipedia.org/wiki/Turing_machine for some more details.

As a sage put it, there's nothing new under the sun.

Offline

Board footer

Powered by FluxBB