Brainfck Programming Language Examples.
This post is all about BrainFuck, Programming Language. For starters, you can get details about this from wikipedia .
Since this is an esoteric programming language, it is well know for its complexity that make programmers amuse and yeah the name says it all 
I had looked into this as a part of Shaastra 2008 IITM . Luckily i was among the 15 finalists.
At first a brief introduction on BrainFuck. This language supports only eight commands and each consisting of only one character. The brainfuck language uses a simple machine model consisting of the program and instruction pointer, as well as an array of at least 30,000 byte cells initialized to zero; a movable data pointer (initialized to point to the leftmost byte of the array); and two streams of bytes for input and output (most often connected to a keyboard and a monitor respectively, and using the ASCII character encoding).
. Output the Byte in the Data Pointer
, Accept one Byte of Data in the current data pointer
> Move the data pointer to right
< Move the data pointer to left
- decrement the data byte at the pointer by one
+ Increment the data byte at the pointer by one
[ if the byte at the data pointer is zero, then instead of moving the pointer forward to the next command, jump it forward to the command after the matching ]
] if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [
Since the language supports only these 8 characters, rest are ignored.
Below you can find the sample questions and the corresponding programs.
Given any number (less than or equal to 64), print it in the binary format. Also, give the output(in decimal format) when the number is multiplied by 3.
Input: 15
Output: 1111
45
,
>
,
>
++++++++++++++++++++++++++++++++++++++++++++++++
[
<
-
< - >>-
]
<<
[
-
]
>
[
-
<>
]
;p0 equals number in one pointer
<< ;p0 for example [- >+ ;p1
< ;p0
]
< ;p255
[<>[-
]]
<< ++++++++++++++++++++++++++++++++++++++++++++++++. ------------------------------------------------ [-] >
[
->+
>
;p0 copied to p1 and p255
> ;p1
[ ;p1
[
-
>>>+<<< [ - >+
>>+
<<
< [- >>+
<< ] ] >>
[-<>
]
<< ] ;p1 p2 will be half p1 > ;p2
[->>>>+<<>[->+<<<>>]
<
[-<>]
<>>>> ;p6
]
<<<<<<<<<<
[
-
++++++++++++++++++++++++++++++++++++++++++++++++.
<<<<< ] ; ++++++++++. ---------- >>>>>[-]
>[-]
>[-]
>[-]
<<<<< ;p255
[
-
>
]
< ;p254 [[-[-[-[-[-[-[-[-[-[- >+ ;p255
< ;p254
[-<>] ;p252
]]]]]]]]]]
<< ;p252 [- >>+ ;p254
<< ;p252 ] >> ;p254
]
> ;p255
[-
>++++++++++ ;p0
>+ ;p1
>+ ;p2
<<< ;p255 ] > ;p0
[-<<>>] ;p253
;p253 is the last diit
>
[->>>+<<>>+<<>>+<<>>+<<>>+<<>>+<<>>+<<>>+<<>>+<<>>+<<< >>+[>[-]] ;p3
<<>>+<<>
++++++++++++++++++++++++++++++++++++++++++++++++.
------------------------------------------------
>
++++++++++++++++++++++++++++++++++++++++++++++++.
------------------------------------------------
<<<<<<<
++++++++++++++++++++++++++++++++++++++++++++++++.
------------------------------------------------
Given a string input, rotate it (last to first) by a specific amount.Input format: first two places will be for amount of rotation.
Input: 04abcdefghij
Output (rotated by 4): ghijabcdef
,
> ;p2
,
> ;p3
++++++++++++++++++++++++++++++++++++++++++++++++
[
< ;p2
-
< ;p1 - >>- ;p3
]
<< ;p1
[
-
;p1
]
> ;p2
[
-
<>
]
>,----------
[
++++++++++>
>,
----------
]
<<
[
<<
]
;at the end this will be p1 for example
< ;p0 mainloopbeGins [ ->>+ ;p2
-
>[>>] ;this end with p11 for example
<<<< ;p7 if non zero loop beGins [ >> ;p9
[
-
>+ ;p10
<
]
<< ;p7to p9 [->>+<>> ;p10
[-<<>>]
<<<<<
] ;p5
;this ends with p1 which is zero
< ;p0 ] >>> ;p3
[
.
>> ;p5
]
Geometric Progressions.Given the values of ‘a’ (first term of the G.P.) and ‘r’ (the common ratio), print the G.P. till 100.Input format: two places for value of ‘a’ followed by two places for value of ‘r’.
Input: 1502
Output: 15
30
Input: 0502
Output: 5
10
20
40
80
>>>>>>>>> ;p10
,
> ;p11
,
> ;p12
,
> ;p13
,
> ;p14
++++++++++++++++++++++++++++++++++++++++++++++++ ;p348
[-
>>>
]
<<<<
[
-
<<<<<<<<>>>>>>>>
]
>
[
-
<<<<<<<<<>>>>>>>>>
]
>
[
-
<<<<<<<<<>>>>>>>>>
]
>
[
-
<<<<<<<<<<>>>>>>>>>>
]
<<<<<<<<<<< ;asci conversion true
< [ ;p1 main loop begins only if the number is not zero [ - >>+ ;p3
<< ;p1to0 ] >> ;p3
[
- ;p3
>>+ ;p5
>+ ;p6
>>+ ;p8
<<<<< ;p3 ] >>
[ ;p5 to minus 10 in steps of 1
[>[-]< ;p6
[-<> ;p3 to p5
[-<>
[-<>
[-<>
[-<>
[-<>
[-<>
[-<>
[-<>
[-<>
[
-
>+ ;p6
< ;p5 ] ]] ]] ]] ]] ] ] >[-]<
]
< ++++++++++++++++++++++++++++++++++++++++++++++++. ------------------------------------------------ > ;p5
]
;up to displaying the fistdigits varified
;now to display second digit
++++++++++ ;p5
< ;p4 ] > ;p5
[
-
<> ;p5
]
<< ;p3
++++++++++++++++++++++++++++++++++++++++++++++++.
[-]
<< ++++++++++. ---------- ;varified for 2 digit ;now to copy common ratio > ;p2
[
-
>+ ;p3
< ;p2 ] > ;p3
[
-
>>+ ;p5
<<
<<<<<<<<>>>>>>>>>> ;p3
]
;common ratio copied succesfully
;to find the corresponding next terms in gp
>>>>>
[
-
<<< ;p5 [ - >>+ ;p7
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
[-[-[-[-[-[-[-[-[-[-
<<<
+++++++++++++++++++++++++++++++++++++++++++++++++.
-------------------------------------------------
>- ;p4
>[-] ;p5
>>>[-]
<
[
-
]
]]]]]]]]]]
]]]]]]]]]]
]]]]]]]]]]
]]]]]]]]]]
]]]]]]]]]]
]]]]]]]]]]
]]]]]]]]]]
]]]]]]]]]]
]]]]]]]]]]
]]]]]]]]]]
<<
<+ ;p3
[
-
>>>
[
-
>+
<
]
<<< ] >> ;p5
]
< ;p4 [ - >+ ;p5
< ] >>>> ;p8
]
<<<<<>>>>
[
-
<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>
]
<<<<
]
<<<<<<<<<<<
]
Sort the given string alphabetically.
Input : harshalpatil
Output: aaahhillprst
>, ;p2
----------
[++++++++++
>>>>, ;p5
----------> ;p6
]
<
[++++++++++
<<
[
<< [ ;if nonzero >>>
[>]
>>+
[
[-]
<<<
[
<< [>+>>
[>]
>>>+
<<<<
[
<
[-
] ;checkin and restorinG
>>
[>]
>
[>
+>>
+<<
[-]
>>
[->
+>+
<
[-
<
]
>>>
[< [>>>-
<<< [-] ] >>
]
<< ] >>>+
[
-
<<<<<< [-] + >>>>>>
]
<
[-]
<
[-]
<
[-]
< [ [>+
]
<<<
[
<< [>>+
<
]
< [ [>+
>>
[>]
>>>+
<<<<
[
<< ] >
[-]
>>
[>]
>>
]
>
[-
[
-]
]
<
]]
<
[
<<
[
>
[>]
>-
]
<<
[
[
[
-
]
>
]
>>,
----------]
<<<<
[
[.
>
]
++++++++++. ;printin in alphabetical order
That’s it. These were the four programs that helped me to get in to the finals


