I've recently taken a renewed interest in non-standard languages like Brainfuck and TIS-100. I'm currently writing a Fibonacci generator in Brainfuck which differs a bit in approach from the usual generators already around.
Since my current understanding of the language is very limited, the limited debugging capabilities in the average Brainfuck IDE and the fact that I've never written a Fibonacci generator before, I decided to write one in Python first.
The usual Fibonacci generator seems to be recursive. If I understand correctly, mine isn't. Since the goal was to produce a generator which would serve as a blueprint for it's Brainfuck equivalent, I tried writing it as straight-forward as possible.
The straight-forwardness means no fancy stuff. Exception handling is for the weak and it's too short to warrant a function just for the sake of defining a function. If this is to be re-used, the whole thing will turn into a function.
Again, it's a blueprint for an implementation in a language where such constructs don't exist.
import sys
HIGHEST_FIBO = int(sys.argv[1])
Fibo = [1, 1, 2]
for i in Fibo:
print i
for i in xrange(HIGHEST_FIBO-len(Fibo)):
Fibo[0] = Fibo[1]
Fibo[1] = Fibo[2]
Fibo[2] = Fibo[0] + Fibo[1]
print Fibo[2]
It's speed surprised me. 2000
under 2 seconds, where print
is likely the most expensive operation. It should be very memory efficient as well.
I think this is about as simple and as clear as it gets, but let's see if there is room for improvement anyway.