/t/ - Science/Technology

For discussion of Science, Technology and Math

[Return] [Go to Bottom] [Catalog]

No.594[D]

I have been messing around with brainfuck out of boredom. It's a pretty cool language, and it's so simple that I can teach it to you right here.
Brainfuck initializes a large array and then operates on one cell at a time. Each character is an instruction, and there are 8 of them:
> Move to next cell
< Move to previous cell
+ Increment current cell
- Decrement current cell
[ Open loop
] Close loop (exits if current cell's value is 0)
, Read a character of input
. Print the current cell to output
This language makes it hard to do simple things. I have written an adder, a multiplier, and a divider. Here is the code for the divider:
,>>>,[-<<+>+>]>>>,[-<<+>+>]<[-<<<<<->->->>>]<<<<<[>[<->-]>>>+<<[->+<]>[-<+<+>>]<<<]>>>>.
It takes three characters of input, where the first is the numerator, the second is the denominator, and the third is the character to be considered 0 (brainfuck uses ASCII values, so an input of the character 0 is normally not equal to a value of 0). The program only resolves if the numbers are evenly divisible, but it is accurate and I'm proud to have managed to put it together.
You can run it and try writing your own brainfuck code here:
https://tio.run/#brainfuck
What can you put together? I'd be really impressed by something like a factorial function or a Fibonacci sequence generator. Have fun and stay comfy, anons.

No.595[D][DF]

>>594
Posting again because I managed to make a Fibonacci sequence generator:
>+>>,>>>,[-<+<+>>]<<[-<->]<[-<<<[->>+<<]>[-<+>>+<]>[-<+>]>]<<<[->>>>>+<<<<<]>>>>>.
It takes the an argument of two characters, the first being the index of the desired Fibonacci number (0-indexed from [1, 1, 2, 3, 5, etc.]), and the second being the number to consider as 0 for input/output purposes. I expected this to be harder than the divider, but it ended up being easier. I still think that a factorial function would be hard, though.

No.596[D][DF]

>>595
Correction:
[0, 1, 1, 2, 3, 5...]

No.598[D][DF]

>>594
I did it! This function computes the factorial of its input (with the second character of input being 0, as before).
,>>>>>>,[->+>+<<]>[-<<<<<<<->>>>>>>]<<<<+<<<[[->+>+<<]>-[-<+>]>[->[->+>+<<]>[-<+>]<<]>[-]>>[-<<+>>]<<<<<]>>>[->>>>>+<<<<<]>>>>>.
This was pretty tricky to figure out, and it took me several attempts. It helped a lot to document what I was trying to do. Debugging in brainfuck is really hard, as you can imagine.
The function isn't actually that useful, since taking the factorial of anything higher than 5 will just give you an overflow (interestingly, the factorial of anything 10 or higher will overflow to exactly 0 on account of being divisible by 256). It's also rather inefficient, running in factorial time due to the way brainfuck works. Regardless, it is totally functional (and yes, it returns the factorial of 0 as 1). Give it a try!
I wonder what's left for me to attempt. Anything more complex than these trivial numerical functions would probably be a big hassle, considering how long this took me.

No.599[D][DF]

>>598
i wonder what the point of this language even is. having a challange/perhaps maybe can have military uses because it's difficult to comprehend unless your a true autist thus making it vital like that old codebreaking stuff?

No.600[D][DF]

>>599
From what I understand, it was originally written to have the smallest possible compiler, so that's why it contains the bare minimum of instructions necessary for Turing-completeness, implemented in the most lightweight manner possible. I'm tempted to try making a compiler or interpreter myself, with how easy it would have to be.
Also, checked.

No.604[D][DF]

heh I wanted to try advent of code this year in brainfuck after seeing anons on /g/ use excel and APL and whatever the shit. Gave up on day 1... Hopefully I'll be able to go for a couple of days next year

[Return] [Go to top] [Catalog]