Fibonacci series in Ada using recursion

Viewed 620

In this code, I am trying to write a program that prints out the Fibonacci series based on the users' input (Index, Size). An then, the program should print out all the Fibonacci numbers between Index..Size. I have trouble, writing a recursion that calculates and prints out the Fibonacci numbers. Any suggestions?

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO, Ada.Unchecked_Deallocation;

procedure Fibonacci is
   type Arr is array (Positive range <>) of Integer;
   type Array_Access is access Arr;
   Size, Index : Positive;
   Variable    : Array_Access;
   procedure Free is new Ada.Unchecked_Deallocation (Arr, Array_Access);

   procedure Recursion (Item : Arr) is                  --Recursion
   begin
      Put_Line
        (Item (Item'First)'Image);                   --Prints out the numbers
      Recursion
        (Item
           (Item'First + Item'First + 1 ..
                Item'Last));     --Calculating the Fibonacci numbers
   end Recursion;

begin
   Put ("Welcome to the Fibonacci number series!");
   Put
     ("Enter an initial value and how many Fibonacci numbers you want to print: ");
   Get (Index);
   Get (Size);
   Variable := new Arr (Index .. Size);
   Recursion (Variable);
end Fibonacci;

Example: Enter Index (the initial value of the Fibonacci series): 1
Enter Size (how many Fibonacci numbers to print): 5
The first 5 Fibonacci numbers are: 1 1 2 3 5

2 Answers

From Wikipedia,

In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
F0 = 0
F1 = 1
and
Fn = Fn - 1 + Fn - 2

which translates pretty directly into

function Fibonacci (N : Natural) return Natural is
  (case N is
      when 0 => 0,
      when 1 => 1,
      when others => Fibonacci (N - 1) + Fibonacci (N - 2));

or, old style,

function Fibonacci (N : Natural) return Natural is
begin
   if N = 0 then
      return 0;
   elsif N = 1 then
      return 1;
   else
      return Fibonacci (N - 1) + Fibonacci (N - 2);
   end if;
end Fibonacci;

You do have to do your printing outside the function, and admittedly there’s an inefficiency in repeatedly calculating the lower results, but you weren’t asking for efficiency.

Here is how you can do it (this code is based on https://rosettacode.org/wiki/Fibonacci_sequence#Recursive)

with Ada.Text_IO;
with Ada.Integer_Text_IO;

procedure Fibonacci is

   First, Amount: Positive;

   function Fib(P: Positive) return Positive is --Recursion
   begin
      if P <= 2 then
         return 1;
      else
         return Fib(P-1) + Fib(P-2);
      end if;
   end Fib;

begin
   Ada.Text_IO.Put_Line("Welcome to the Fibonacci number series!");
   Ada.Text_IO.Put_Line("Enter an initial value and how many Kombinacci numbers you want to print: ");
   Ada.Integer_Text_IO.Get(First);
   Ada.Integer_Text_IO.Get(Amount);
   for I in First .. First + Amount loop
      Ada.Text_IO.Put("Fibonacci(" & Positive'Image(I) & " ) = ");
      Ada.Text_IO.Put_Line(Positive'Image(Fib(I)));
   end loop;
end Fibonacci;
Related