Fibonacci numbers in INTERCAL
This example uses iterative definition of Fibonacci numbers. Variables .10 and .11 store previous and current calculated numbers, and .9 stores the number of iterations left.
Loop body is quite simple: print current number .11, copy .10 and .11 to .1 and .2, add them (
(1009) NEXT calls addition from standard library and puts sum to .3) and update the values. The trickiest part of the program is the code that implements looping behavior. Here is what it does.
(3) NEXT and
(4) NEXT move execution to label (4). At this line the loop counter .9 is updated by subtracting 1 from it (call of
(1010)). After this, .1 is calculated in a rather complicated way that makes it 1 if .9 is non-zero, and 0 otherwise. After this, .1 is incremented to be 1 if the loop has to stop (loop counter is zero) and 2 otherwise. Finally,
RESUME .1 is performed to return to one of the
NEXTs applied. If
.1 is 2, the program returns two
NEXTs back, and continues with
DO (1) NEXT which brings it to the start of the loop again. However, if
.1 is 1, the program returns one
NEXT back, continues with
PLEASE GIVE UP and halts.
Note that numbers are printed in Roman notation (this language lets not a single thing be easy!), one number per two lines (line which is empty in this example is for modifiers), so the output looks like this:
DO .9 <- #16 DO .10 <- #0 DO .11 <- #1 (1) PLEASE READ OUT .11 DO .1 <- .10 DO .2 <- .11 PLEASE (1009) NEXT DO .10 <- .11 DO .11 <- .3 DO (3) NEXT DO (1) NEXT (3) DO (4) NEXT PLEASE GIVE UP (4) DO .1 <- .9 DO .2 <- #1 PLEASE (1010) NEXT DO .9 <- .3 DO .1 <- '.9~.9'~#1 PLEASE (1020) NEXT DO RESUME .1