patternMinor
Fibonacci Sequence in BrainF***
Viewed 0 times
brainffibonaccisequence
Problem
I have been trying to write a BrainF*** program that prints the Fibonacci sequence numbers repeatedly. I was wondering whether this is the most efficient way to do it. I basically repeatedly duplicate two cells into a third cell, and shift one cell forward and repeat.
+++++[->----[---->+++.-[++++>---.++.---------.+++.[++>-----.++[->+++.+++++++++..---.+++++++.+[-->+++++-.<]Solution
Portability
Your first loop immediately subtracts 4 from a cell whose value is 0. It seems like your program is designed to run using the wraparound dialect of Brainfuck, so it would be a good idea to note that in a comment. On the other hand, writing a Fibonacci sequence generator using the 8-bit dialect isn't that useful, because you would overflow after 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233.
However, I'm puzzled by why you would want to do any subtraction at all. After all, the Fibonacci sequence involves addition, not subtraction.
Readability
There's no need to cram everything on one line. Best practice would be to insert line breaks after
Suggested solution
Your first loop immediately subtracts 4 from a cell whose value is 0. It seems like your program is designed to run using the wraparound dialect of Brainfuck, so it would be a good idea to note that in a comment. On the other hand, writing a Fibonacci sequence generator using the 8-bit dialect isn't that useful, because you would overflow after 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233.
However, I'm puzzled by why you would want to do any subtraction at all. After all, the Fibonacci sequence involves addition, not subtraction.
Readability
There's no need to cram everything on one line. Best practice would be to insert line breaks after
. commands and use indentation to show off the loop structure. Add some spaces as well, to let your code breathe.Suggested solution
++++++++++ [->
+++>+++++++>++++++++++>+++++++++++>++++++++++++ > +++ .
>> ++ .
++++ .
> + .
>> .
> + .
> ----- .
.
--- .
<<< + .Code Snippets
++++++++++ [->
+++>+++++++>++++++++++>+++++++++++>++++++++++++ <<<<<
]
>> +++ .
< ++ .
>>> ++ .
< +++++ .
> ++++ .
> + .
<<<< .
>>> .
< - .
--- .
<< .
>> + .
> ----- .
.
--- .
<<< + .Context
StackExchange Code Review Q#124467, answer score: 9
Revisions (0)
No revisions yet.