Brainfuck challenge

1.6k views Asked by At

I have a any challenge. I must write brainfuck-code.

For a given number n appoint its last digit .

entrance

Input will consist of only one line in which there is only one integer n ( 1 < = n < = 2,000,000,000 ) , followed by a newline ' \ n' (ASCII 10).

exit

On the output has to find exactly one integer denoting the last digit of n .

example I entrance: 32 exit: 2

example II: entrance: 231231132 exit: 2

This is what I tried, but it didn't work:

+[>,]<.>++++++++++.
4

There are 4 answers

0
Syméon Smith On

Nope sorry, real answer is

,[>,]<.

because your answer was getting one too far ;)

0
Sirmyself On

Depending on the interpreter, you might have to escape the return key by yourself. considering the return key is ASCII: 10, your code should look like this :

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

broken down :

>               | //first operation (just in case your interpreter does not
                    support a negative pointer index)
,----- -----    | //first entry if it's a return; you don't even get in the loop
[                
    +++++ +++++ | //if the value was not ASCII 10; you want the original value back
    >,          | //every next entry
    ----- ----- | //check again for the the return, 
                    you exit the loop only if the last entered value is 10
]
<.              | //your current pointer is 0; you go back to the last valid entry
                    and you display it
0
Dorian On

The last input is the newline. So you have to go two memory positions back to get the last digit of the number. And maybe you don't have to return a newline character, so the code is

,[>,]<<.
0
AIDAN GREEN On

Your issue is that a loop continues for forever until at the end of the loop the cell the pointer is currently on in equal to 0. Your code never prints in the loop, and never subtracts, so your loop will never end, and all that your code does is take an ASCII character as input, move one forward, take an ASCII character as input, and so on. All of your code after the end of the loop is useless, because that your loop will never end.