Ada Programming/Algorithms/Chapter 6

Fibonacci numbers
The following codes are implementations of the Fibonacci-Numbers examples.

Simple Implementation
...

To calculate Fibonacci numbers negative values are not needed so we define an integer type which starts at 0. With the integer type defined you can calculate up until. will result in an.

Integer_Type  0 .. 999_999_999_999_999_999;

You might notice that there is not equivalence for the  from the original example. Ada will test the correctness of the parameter before the function is called.

Fib (n : Integer_Type) Integer_Type n = 0 0;      n = 1 1;         Fib (n - 1) + Fib (n - 2); ;   Fib; ...

Cached Implementation
...

For this implementation we need a special cache type can also store a -1 as "not calculated" marker

Cache_Type  -1 .. 999_999_999_999_999_999;

The actual type for calculating the fibonacci numbers continues to start at 0. As it is a of the cache type Ada will automatically convert between the two. (the conversion is - of course - checked for validity)

Integer_Type Cache_Type 0 .. Cache_Type';

In order to know how large the cache need to be we first read the actual value from the command line.

Value : Integer_Type := Integer_Type'Value (Ada.Command_Line.Argument (1));

The Cache array starts with element 2 since Fib (0) and Fib (1) are constants and ends with the value we want to calculate.

Cache_Array (Integer_Type 2 .. Value)  Cache_Type;

The Cache is initialized to the first valid value of the cache type &mdash; this is.

F : Cache_Array := ( => Cache_Type'First);

What follows is the actual algorithm.

Fib (N : Integer_Type) Integer_Type N = 0  N = 1 N;      F (N) /= Cache_Type'First F (N); F (N) := Fib (N - 1) + Fib (N - 2); F (N); ;   Fib; ...

This implementation is faithful to the original from the Algorithms book. However, in Ada you would normally do it a little different:

when you use a slightly larger array which also stores the elements 0 and 1 and initializes them to the correct values

Cache_Array (Integer_Type 0 .. Value)  Cache_Type; F : Cache_Array := (0     => 0,       1      => 1,        => Cache_Type'First);

and then you can remove the first path.

N = 0  N = 1 N;     els  F (N) /= Cache_Type'First

This will save about 45% of the execution-time (measured on Linux i686) while needing only two more elements in the cache array.

Memory Optimized Implementation
This version looks just like the original in WikiCode.

Integer_Type  0 .. 999_999_999_999_999_999;   Fib (N : Integer_Type)  Integer_Type U : Integer_Type := 0; V : Integer_Type := 1; I  2 .. N         Calculate_Next : T : Integer_Type := U + V;            U := V;            V := T;          Calculate_Next; ;      V;    Fib;

No 64 bit integers
Your Ada compiler does not support 64 bit integer numbers? Then you could try to use decimal numbers instead. Using decimal numbers results in a slower program (takes about three times as long) but the result will be the same.

The following example shows you how to define a suitable decimal type. Do experiment with the and  parameters until you get the optimum out of your Ada compiler.

Integer_Type  1.0  18 0.0 .. 999_999_999_999_999_999.0;

You should know that floating point numbers are unsuitable for the calculation of fibonacci numbers. They will not report an error condition when the number calculated becomes too large &mdash; instead they will lose in precision which makes the result meaningless.

|Chapter 6