3
votes
\$\begingroup\$

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.

+++++[->----[---->+<]>++.-[++++>---<]>.++.---------.+++.[++>---<]>--.++[->+++<]>.+++++++++..---.+++++++.+[-->+++++<]>-.<]
\$\endgroup\$

locked by 200_success Apr 2 '16 at 15:38

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. See the help center for guidance on writing a good question.

Read more about locked posts here.

  • 1
    \$\begingroup\$ This code causes a time-out in all environments I've tried so far. What environment are you using that makes this work? \$\endgroup\$ – Mast Apr 1 '16 at 18:01
  • 5
    \$\begingroup\$ @Mast That would be due to the portability issue that I pointed out in my answer. \$\endgroup\$ – 200_success Apr 1 '16 at 18:20
  • \$\begingroup\$ @Mast I am using a standard brainf*** interpreter (I run bf <file>). \$\endgroup\$ – thepiercingarrow Apr 2 '16 at 0:59
  • 1
    \$\begingroup\$ Note (hint) to the reader: check the date of posting to understand the "historical significance", and why it's not considered a normally acceptable question ;-) \$\endgroup\$ – janos Apr 2 '16 at 16:11
9
votes
\$\begingroup\$

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 . commands and use indentation to show off the loop structure. Add some spaces as well, to let your code breathe.

Suggested solution

++++++++++ [->
   +++>+++++++>++++++++++>+++++++++++>++++++++++++ <<<<<
]
>> +++ .
< ++ .
>>> ++ .
< +++++  .
> ++++ .
> + .
<<<< .
>>> .
< - .
--- .
<< .
>> + .
> ----- .
.
--- .
<<< + .
\$\endgroup\$
  • 4
    \$\begingroup\$ I find it amusing you're talking about readability in a language that's specifically designed to be hard to read. I mean, it's right there in the name... \$\endgroup\$ – Darrel Hoffman Apr 1 '16 at 20:23
  • 5
    \$\begingroup\$ @DarrelHoffman If that's what you think, then please allow me to further correct your misconceptions of the language! \$\endgroup\$ – 200_success Apr 1 '16 at 20:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.