r/brainfuck • • Feb 16 '20

Fibonacci Printer

Use with a cell size as high as possible to avoid overflow/rollover.

>+[[>+>+<<-]<[>>+<<-]>>[<+>-]>[<<<+>>>-]<<[->+>+<<]>>[-<<+>>]++++++++++>>>>>+<<<<<<[[->->+<[>-<[->>+<<]]>>[-<<+>>]<[-<++++++++++>>>+<<]<<]>>>>>>[>]+++++++++++[<]<<<<[->>>>>[>]<-[<]<<<<]>>>[-<<<<+>>>>]<<<++++++++++<]>[-]>>>>>->[>]<[>++++++[-<++++++++>]<-.[-]<]++++++++++.[-]<<<<<<<]
1 Upvotes

5 comments sorted by

2

u/danielcristofani Feb 16 '20 edited Feb 16 '20

Big cell sizes won't help you much. Say you use 128-bit cells. This would give an inaccurate result for F(187) (should be 538522340430300790495419781092981030533), except that it would take it many quintillions of years to get that far.

The right answer is to divide the numbers across multiple cells, so you can compute the first 1000 Fibonacci numbers in one second and then keep going at full speed. Fortunately this isn't too hard. I've been writing up detailed comments on a series of my programs; that one is at http://www.hevanet.com/cristofd/brainfuck/fib_explained.b

2

u/RojerGS Feb 27 '20

How did you conclude the result for the program above would be incorrect?

2

u/danielcristofani Feb 27 '20

That's the first Fibonacci number that exceeds 2128, so it won't fit in a 128-bit cell. More relevant is that we'll all be dead and the sun burned out long before then, though. It's a slow algorithm.

1

u/RojerGS Feb 27 '20

Ah I understand. For some reason I thought you meant that the algorithm had some inaccuracy that would show after some terms

2

u/danielcristofani Feb 27 '20

It does, but the number of terms before it gives inaccurate answers is dependent on the cell size. :)