How to resolve the algorithm Fibonacci sequence step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fibonacci sequence step by step in the Cowgol 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 Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";

sub fibonacci(n: uint32): (a: uint32) is
    a := 0;
    var b: uint32 := 1;
    while n > 0 loop
        var c := a + b;
        a := b;
        b := c;
        n := n - 1;
    end loop;
end sub;

# test
var i: uint32 := 0;
while i < 20 loop
    print_i32(fibonacci(i));
    print_char(' ');
    i := i + 1;
end loop;
print_nl();

  

You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Elixir programming language
You may also check:How to resolve the algorithm Hunt the Wumpus step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the QB64 programming language
You may also check:How to resolve the algorithm Box the compass step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Levenshtein distance step by step in the Arc programming language