How to resolve the algorithm Fibonacci sequence step by step in the Oberon-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fibonacci sequence step by step in the Oberon-2 programming language

Table of Contents

Problem Statement

The Fibonacci sequence is a sequence   Fn   of natural numbers defined recursively:

Write a function to generate the   nth   Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion). The sequence is sometimes extended into negative numbers by using a straightforward inverse of the positive definition: support for negative     n     in the solution is optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fibonacci sequence step by step in the Oberon-2 programming language

Source code in the oberon-2 programming language

MODULE Fibonacci;
IMPORT
  Out := NPCT:Console;

PROCEDURE Fibs(VAR r: ARRAY OF LONGREAL);
VAR
  i: LONGINT;
BEGIN
  r[0] := 1.0; r[1] := 1.0;
  FOR i := 2 TO LEN(r) - 1 DO
    r[i] := r[i - 2] + r[i - 1];
  END
END Fibs;

PROCEDURE FibsR(n: LONGREAL): LONGREAL;
BEGIN
  IF n < 2. THEN
    RETURN n
  ELSE
    RETURN FibsR(n - 1) + FibsR(n - 2)
  END
END FibsR;

PROCEDURE Show(r: ARRAY OF LONGREAL);
VAR
  i: LONGINT;
BEGIN
  Out.String("First ");Out.Int(LEN(r),0);Out.String(" Fibonacci numbers");Out.Ln;
  FOR i := 0 TO LEN(r) - 1 DO
    Out.LongRealFix(r[i],8,0)
  END;
  Out.Ln
END Show;

PROCEDURE Gen(s: LONGINT);
VAR
  x: POINTER TO ARRAY OF LONGREAL;
BEGIN
  NEW(x,s);
  Fibs(x^);
  Show(x^)  
END Gen;

PROCEDURE GenR(s: LONGINT);
VAR
  i: LONGINT;
BEGIN
  Out.String("First ");Out.Int(s,0);Out.String(" Fibonacci numbers (Recursive)");Out.Ln;
  FOR i := 1 TO s DO  
    Out.LongRealFix(FibsR(i),8,0)
  END;
  Out.Ln  
END GenR;

BEGIN 
  Gen(10);
  Gen(20);
  GenR(10);
  GenR(20);
END Fibonacci.

  

You may also check:How to resolve the algorithm Sudoku step by step in the VBScript programming language
You may also check:How to resolve the algorithm Summarize and say sequence step by step in the Scala programming language
You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Prolog programming language