Writing a programme to calculate Fibonacci numbers is nowadays an exercise in triviality but it is not futile to examine various ways to implement this calculation and look at how different languages offer other, perhaps more elegant ways to generate the sequence.

In what follows only the heart of the Fibonacci algorithm is shown. Embedding it in a loop to generate the entire sequence is not considered nor is the need for arbitrary precision arithmetic, which is available for most languages nowadays.

The closed form solution for the n’th Fibonacci number is not considered nor is parallelisation of sequence generation and, with the exception of Forth, only some established mainstream languages have been considered at this stage.

The Fibonacci sequence is defined by the transformation of an ordered pair

`(x,y) --> (x+y,x) with x >=y and y >=0`

The first algorithm most people think
of involves putting x into a temporary location
`x --> temp `

`x --> x+y `

`temp -->x`

Or alternatively
`swap(x,y) x --> x+y`

We will call this the traditional
algorithm. However other languages suggest other algorithms **Java**

**as**mutable arrays. This allows an algorithm that does not use temporary storage and which cleans up after itself. Transitioning to Java Big Integers is straightforward. There seems to be no advantage to adopting a Java 8 stream based approach here.

// fibs is a list initialised with two elements and is then transformed as shown below. public static List<Integer> nextFib(List<Integer> fibs) { fibs.add(fibs.get(0) + fibs.get(1)); fibs.remove(0); return fibs; }

` `

To generate a list with the entire
sequence, till the memory runs out
public static List<Integer> nextFib(List<Integer> fibs) { Long len = fibs.size(); fibs.add(fibs.get(len-2) + fibs.get(len-1)); return fibs; }

Fortran

`code than Fortran prompting a move to C.`

Fortran seems only to allow the traditional algorithm and, at least using Eclipse for Parallel Application developers, requires a more boilerplate code than Java. The advantage of Fortran is speed, though again C has largely caught up. The code here puts a subroutine into a module and calls it from a main program thus altering the state of the input array. Fortran therefore offers little interest in this context but is included for historical reasons

! Given f(n-1) and f(N-2) compute f(n) ! via F(n) = F(N-1) + F(N-2) ! Using an array of length two works round the fact that ! Fortran can’t return anything from a subroutine ! And does not have lists like Java module Fibonacci contains SUBROUTINE FIB( F) integer, dimension(:) :: F INTEGER TEMP TEMP = SUM(F) F(2) = F(1) F(1) = TEMP RETURN END subroutine FIB end module Fibonacci

program fibonacci use Fibonacci implicit none INTEGER, DIMENSION(2) :: Fibo Fibo = (/ 2, 1/) call FIB(Fibo) print *, Fibo end program Fibonacci

` `

**C**

#include<stdio.h> // given F(N-1) and F(n-2) produces F(n) // MUST BE CALLED WITH REFERENCES NOT ACTUAL VALUES. // JAVA IS ALSO CALL BY REFERENCE void fib( int* Fnminusone, int* FnMinustwo) { int temp = *Fnminusone; *Fnminusone += *FnMinustwo; *FnMinustwo = temp;

*}*

main() { // X = f(N-1) Y = f(N-2) int x =3; int y =2; fib(&x, &y); printf("%d %d", x, y); }

` `

**Python**

This is fast, simple and elegant. Also Python has arbitrary precision built in.

def nextFibs(x,y): return x+y,x

` `

` `

**Forth**

typing a number in the FORTH interpreter puts it onto the stack:NextFibs dup rot + ;

dup duplicates the value on top of the stack and puts it on the stack. so 2 dup results in the stack holding 2 2

rot does a cyclic shift of the stack

+ takes the two numbers at the top of the stack, adds them and puts the result on the stack

so for example 2 3 + results in the stack holding 5

Thus 3 2 NextFibs does the following

dup: 3 2-->3 3 2

rot: 3 3 2–->3 2 3

+: 3 2 3 --> 5 3

**Conclusions**

algorithm is not quite so lean but easier to understand. Java Lists allow an algorithm that does not involve temporary storage and is fairly lean, though overall Java is a verbose language. Fortran shows its age and C, being call by reference like Java, uses pointers and references which many find confusing nowadays.

Assuming you want or need to program the sequence Python will allow fastest development, Java and Python will allow writing to files easily but Python scores having transparent arbitrary precision arithmetic. Forth, with an appropriate library may be the fastest option but tends to be a write-only language and will need good commenting.

The take home from this is that an apparently simple transformation like that involved for the Fibonacci sequence may be implemented in different ways in different languages and some languages allow more elegant implementations than others. When implementing a solution to a problem it is worth choosing the language to match the problem ( if you have a choice) and to look at different ways to solve the problem.