How to resolve the algorithm Fibonacci n-step number sequences step by step in the Ada programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fibonacci n-step number sequences step by step in the Ada programming language
Table of Contents
Problem Statement
These number series are an expansion of the ordinary Fibonacci sequence where: For small values of
n
{\displaystyle n}
, Greek numeric prefixes are sometimes used to individually name each series. Allied sequences can be generated where the initial values are changed:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fibonacci n-step number sequences step by step in the Ada programming language
Source code in the ada programming language
package Bonacci is
type Sequence is array(Positive range <>) of Positive;
function Generate(Start: Sequence; Length: Positive := 10) return Sequence;
Start_Fibonacci: constant Sequence := (1, 1);
Start_Tribonacci: constant Sequence := (1, 1, 2);
Start_Tetranacci: constant Sequence := (1, 1, 2, 4);
Start_Lucas: constant Sequence := (2, 1);
end Bonacci;
package body Bonacci is
function Generate(Start: Sequence; Length: Positive := 10) return Sequence is
begin
if Length <= Start'Length then
return Start(Start'First .. Start'First+Length-1);
else
declare
Sum: Natural := 0;
begin
for I in Start'Range loop
Sum := Sum + Start(I);
end loop;
return Start(Start'First)
& Generate(Start(Start'First+1 .. Start'Last) & Sum, Length-1);
end;
end if;
end Generate;
end Bonacci;
with Ada.Text_IO, Bonacci;
procedure Test_Bonacci is
procedure Print(Name: String; S: Bonacci.Sequence) is
begin
Ada.Text_IO.Put(Name & "(");
for I in S'First .. S'Last-1 loop
Ada.Text_IO.Put(Integer'Image(S(I)) & ",");
end loop;
Ada.Text_IO.Put_Line(Integer'Image(S(S'Last)) & " )");
end Print;
begin
Print("Fibonacci: ", Bonacci.Generate(Bonacci.Start_Fibonacci));
Print("Tribonacci: ", Bonacci.Generate(Bonacci.Start_Tribonacci));
Print("Tetranacci: ", Bonacci.Generate(Bonacci.Start_Tetranacci));
Print("Lucas: ", Bonacci.Generate(Bonacci.Start_Lucas));
Print("Decanacci: ",
Bonacci.Generate((1, 1, 2, 4, 8, 16, 32, 64, 128, 256), 15));
end Test_Bonacci;
You may also check:How to resolve the algorithm Euler method step by step in the Haskell programming language
You may also check:How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the C programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the REXX programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the PL/M programming language
You may also check:How to resolve the algorithm Generic swap step by step in the EchoLisp programming language