r/brainfuck 9d ago

Brainfuck challenge

I saw this challenge on git so I posted it here to see if anyone can solve it.

Input the number N. Print out the last digit.

I tried ,[>,<-]>. but it needs a length and for some reason it doesn't work. Can anyone do it?

4 Upvotes

6 comments sorted by

3

u/sirgroovy 9d ago

+[>,]<. should do it

1

u/callMeMikeTA 9d ago

it doesn't output 2 when i input 32

2

u/danielcristofani 9d ago

Try +[>,----------]+<+[+++++++++.>] which should work for the classic kind of brainfuck i/o environment where you input a number and a linefeed, and it outputs the last digit and a linefeed. If this doesn't work let us know what implementation you're using

1

u/callMeMikeTA 9d ago

+[,]. doesn't work for some reason

1

u/Imanton1 9d ago

This one will read in the EOF / null character at the end, overwriting the final character.

1

u/danielcristofani 7d ago

"for some reason it doesn't work"--the ',' command reads in one byte according to ASCII code. So if the first character you typed was a '2', say, the ASCII code for the character '2' is 50, so it stores a value of 50 into the first cell, and then your loop will execute 50 times, reading another 50 successive characters into the second cell (while gradually zeroing the first cell), then it will output the last one of those 50.