13 chars of Golfscript:
2,~{..p@+.}do
Update to explain the operation of the script:
2,
makes an array of [0 1]
~
puts that array on the stack
- So, at the time we run the
do
, we start the stack off with 0 1
(1 at top of stack)
The do
loop:
- Each
.
duplicates the top item of the stack; here, we do this twice (leaving us with 0 1 1 1
on initial run)
p
prints the topmost value (leaving us with 0 1 1
)
@
rotates the top 3 items in the stack, so that the third-topmost is at the top (1 1 0
)
+
adds the top 2 items in the stack (leaving 1 1
)
.
duplicates the top value, so that the do
loop can check its truthiness (to determine whether to continue)
Tracing this mentally a couple of loops will be enough to tell you that this does the required addition to generate the Fibonacci sequence values.
Since GolfScript has bignums, there will never be an integer overflow, and so the top-of-stack value at the end of the do
loop will never be 0. Thus, the script will run forever.