Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print numbers from 1 to 10 using a loop in Brainfuck? Is it even possible?

How to print numbers from 1 to 10 using a loop in Brainfuck? Is it even possible?
I am looking for a solution to this issue.

like image 343
schrift94 Avatar asked Jan 28 '23 02:01

schrift94


2 Answers

+++++++++++++++++++++++++++++++++++++++++++++++++  Cell 0 to '1'
>++++++++++  cell 1 to '\n'
>+++++++++  cell 2 to 9 as counter
[  Print numbers 1 to 9
<<  Data pointer to cell 0
.+  Print and increment cell 0
>.  Data pointer to cell 1 and print the newline
>-  Data pointer to cell 2 and decrement counter
]  Loop till counter is 0
+++++++++  Set cell 2 to 9
[  Set cell 0 to '1'
<<-  Data pointer to cell 0 and decrement
>>-  Data pointer to cell 2 and decrement counter
]  Loop till counter is 0
<<.  Data pointer to cell 0 and print '1'
-.   Decrement cell 0 and print '0'
>.   Data pointer to cell 1 and print newline

Readable version:

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

Output:

1
2
3
4
5
6
7
8
9
10

Live demo:

Brainf**k print 1 to 10
Brainf**k Visualizer

like image 172
Swordfish Avatar answered Feb 02 '23 15:02

Swordfish


TL;DR

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

Try it online!

END TL;DR

In order to program in BrainF**k, pretend like every program (even simple ones) will need to start out with a layout.

The pseudo-code for this would be something like:

Generate the character '0'
Move left and generate '\n'
Move left and generate the counter (10 numbers in this case)
Loop: Get back to the character '0', print it, increment it to '1', go to the newline, print it, go to the counter, and decrement it. End it when the counter is 0
Generate '1' and print it
Generate '0' and print it

However, the last two steps could be simplified to just:

Go back to the digit '9'
Decrement it until '1' and print
Decrement it until '0' and print

This saves a lot of time and bytes characters.

To generate the character '0', you generate the integer 48 (because that's it's ASCII value). To do this you can go to Esolangs' BF Constants. Looking up the number 48, we find -[>+<-----]>---

Our program so far is -[>+<-----]>--- to generate 0

Next, move left and generate \n (newline). We can use <++++++++++. Notice how it is completely plus signs. This is because there is not much room to reduce the character count at the number 10.

Our program so far is -[>+<-----]>---<++++++++++

Then, move left and generate the counter. We want the counter to be 10 to print numbers from 0 to 9. <++++++++++.

Our program so far is -[>+<-----]>---<++++++++++<++++++++++

After that, start the loop [. Go to the '0' >>, print it ., increment it +, go to the newline and print <., Go to the counter and decrement it, and end the loop when it is zero <-]. [>>.+<.<-]

Our program so far is -[>+<-----]>---<++++++++++<++++++++++[>>.+<.<-]

Finally, go to the '9' >>, decrement it until it is 1 and print ---------., and decrement it until it is 0 and print -.. ---------.-.

The program is finished.

like image 31
MilkyWay90 Avatar answered Feb 02 '23 17:02

MilkyWay90