Fibonacci Sequence Programming Tutorial

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Fibonacci is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. It is a popular problem in computer science and mathematics.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/recursion_dynamic -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} python/conditional_statements -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} python/for_loops -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} python/lists -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} python/tuples -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} python/function_definition -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} python/classes_objects -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} python/encapsulation -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} python/build_in_functions -.-> lab-268857{{"`Fibonacci Sequence Programming Tutorial`"}} end

Fibonacci

Problem

The problem is to implement the Fibonacci sequence in three different ways: recursively, dynamically, and iteratively. The sequence should start at 0 and 1, and the inputs are valid non-negative integers. Both recursive and iterative solutions should be implemented. The program should fit in memory.

Requirements

To solve this problem, the following requirements should be met:

  • The sequence should start at 0 and 1.
  • The inputs are valid non-negative integers.
  • The program should implement both recursive and iterative solutions.
  • The program should fit in memory.

Example

The following are the expected outputs for some input values:

  • n = 0 -> 0
  • n = 1 -> 1
  • n = 6 -> 8
  • Fib sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...

Summary

In summary, the Fibonacci sequence is a popular problem in computer science and mathematics. The problem is to implement the sequence in three different ways: recursively, dynamically, and iteratively. The program should start at 0 and 1, and the inputs should be valid non-negative integers. Both recursive and iterative solutions should be implemented, and the program should fit in memory.

Other Algorithm Tutorials you may like