Prints '+' to the screen 20 times using a loop and arithmetic operators

187 views Asked by At

I have a simple Brainfuck program called "PuraPuraNanya", which prints the character + to the screen 20 times using a loop and arithmetic operators.

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

Here are explanations of what the program does:

  • ++++++++++ initializes the value of the current memory cell to 10.
  • [ starts a loop.
  • >+ moves the value of the current memory cell to the next cell and increments it by 1.
  • >+++ moves the value of the current memory cell to the second next cell and increments it by 3.
  • >++++ moves the value of the current memory cell to the third next cell and increments it by 4.
  • >+++++ moves the value of the current memory cell to the fourth next cell and increments it by 5.
  • <<<<- moves the cursor to the first memory cell and decrements its value by 1.
  • ] closes the loop.
  • >++ moves the cursor to the next memory cell and increments its value by 2.
  • . prints the character + to the screen.

I expected the program to print the character + to the screen 20 times. However, when I ran the program, there were no errors and the compiler did not produce the expected output. Can anyone help me check and identify where the mistake is?

Test :

2

There are 2 answers

2
Daniel Cristofani On BEST ANSWER

So. The memory starts as all 0s. Before you start the loop, memory is:

10 0 0 0 0 ...

The > commands don't move values as you said, they only move the pointer. Then you increment some cells. So after the first time through the loop, your memory will be:

9 1 3 4 5

with the pointer back at the (just decremented) 9. After the second time through,

8 2 6 8 10

After the 10th iteration of the loop, memory is

0 10 30 40 50

and the loop terminates because you ended at a 0 (at the leftmost cell).

Then you move right one cell and increment twice. This makes a 12:

0 12 30 40 50

Then you output a 12.

Unfortunately, 12 is the ASCII code for "form feed", a control character and not a printable character. What that looks like will depend on your interpreter/compiler.

If you wanted to output a + you would want to output a 43 and not a 12. And if you wanted to output it 20 times you would probably want to use a loop to do that; maybe you want one loop to help build a 20 and a 43, and then another loop to output the 43 20 times while decrementing the 20.

Alternatively, you could just put 43 (+) commands and then 20 (.) commands, and no loops.)

0
Tirtharaj Ghosh On

An equivalent code in python which I think will be more self-explanatory than any written description, is -

i = 0
array = [0]*5
array[0] = 10
while array[0] != 0:
    i=i+1
    array[i] += 1
    i=i+1
    array[i] += 3
    i=i+1
    array[i] += 4
    i=i+1
    array[i] += 5
    i=i-4
    array[i] -= 1
i=i+1
array[i] += 2
# print(array[i]) -- prints 12 which is the ASCII for new page
print(chr(array[i]))

So if you dry run, you can observe that inside the loop, the memory cell will be updating as following -

[9] [1] [3] [4] [5]
[8] [2] [6] [8] [10] 
[7] [3] [9] [12] [15] 
[6] [4] [12] [16] [20] 
[5] [5] [15] [20] [25] 
[4] [6] [18] [24] [30] 
[3] [7] [21] [28] [35] 
[2] [8] [24] [32] [40] 
[1] [9] [27] [36] [45] 
[0] [10] [30] [40] [50]

After exiting from loop, increment the 2nd cell by 2

[0] [12] [30] [40] [50]

So now the cursor is in 2nd cell, therefore it is trying to print ASCII value of 12 i.e. new page. See reference.

To know more about this crazy esoteric programming language, see this.