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

Published on 12 May 2024 09:40 PM

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

Source code in the dart programming language

int fib(int n) {
  if (n==0 || n==1) {
    return n;
  }
  var prev=1;
  var current=1;
  for (var i=2; i<n; i++) {
    var next = prev + current;
    prev = current;
    current = next;    
  }
  return current;
}

int fibRec(int n) => n==0 || n==1 ? n : fibRec(n-1) + fibRec(n-2);

main() {
  print(fib(11));
  print(fibRec(11));
}

  

You may also check:How to resolve the algorithm Leonardo numbers step by step in the Odin programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the jq programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Nim programming language
You may also check:How to resolve the algorithm HTTP step by step in the Julia programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the AArch64 Assembly programming language