Fibonacci Series In Dart

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. It is important in various fields, including mathematics, computer science, and even nature, where it can describe patterns of growth. Understanding the Fibonacci series helps in algorithm development, recursive programming, and even in solving complex problems.

What is Fibonacci Series?

The Fibonacci series is defined as follows:

  • The first two numbers in the series are 0 and 1.
  • Each subsequent number is the sum of the previous two numbers.

For example, the series starts as 0, 1, 1, 2, 3, 5, 8, 13, and continues indefinitely. It is used in algorithms for dynamic programming, data structures, and in modeling natural phenomena such as the branching of trees, the arrangement of leaves on a stem, and the fruit sprouts of a pineapple.

Syntax

Example

// Function to generate Fibonacci series up to n terms
List<int> fibonacci(int n) {
  List<int> series = [0, 1]; // Initialize the series with the first two numbers
  for (int i = 2; i < n; i++) { // Start from the third number
    series.add(series[i - 1] + series[i - 2]); // Add the last two numbers
  }
  return series; // Return the complete series
}

How It Works

  1. We start by initializing a list to store the Fibonacci series with the first two numbers, 0 and 1.
  2. We then use a loop to calculate the next numbers in the series.
  3. In each iteration, we add the last two numbers of the list to get the next number, which we append to the list.
  4. Finally, we return the complete list containing the Fibonacci series.
  5. Example 1: Basic Usage

    Example
    
    void main() {
      int n = 10; // Number of terms to generate
      List<int> fibSeries = fibonacci(n); // Call the fibonacci function
      print(fibSeries); // Print the Fibonacci series
    }
    
    List<int> fibonacci(int n) {
      List<int> series = [0, 1]; // Start the series
      for (int i = 2; i < n; i++) {
        series.add(series[i - 1] + series[i - 2]); // Calculate next term
      }
      return series; // Return the series
    }
    

Output:

Output

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Explanation: This code generates the first 10 terms of the Fibonacci series and prints them.

Example 2: Intermediate Usage with Variations

Example

void main() {
  int n = 15; // Generate 15 terms
  List<int> fibSeries = fibonacci(n); // Get Fibonacci series
  print("Fibonacci Series of $n terms: $fibSeries"); // Print with message
}

List<int> fibonacci(int n) {
  if (n <= 0) return []; // Return empty if n is 0 or less
  List<int> series = [0]; // Start with the first term
  if (n > 1) series.add(1); // Add second term if n is more than 1
  for (int i = 2; i < n; i++) {
    series.add(series[i - 1] + series[i - 2]); // Compute next term
  }
  return series; // Return the series
}

Output:

Output

Fibonacci Series of 15 terms: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]

Explanation: This example shows how to handle cases where the input is less than or equal to zero, and prints a formatted output message.

Example 3: Real-World Application

Example

void main() {
  int n = 20; // Number of Fibonacci numbers to generate
  List<int> fibSeries = fibonacci(n); // Generate series
  print("The Fibonacci series can be used in algorithms: $fibSeries");
}

List<int> fibonacci(int n) {
  List<int> series = [0, 1]; // Start the list
  for (int i = 2; i < n; i++) {
    series.add(series[i - 1] + series[i - 2]); // Calculate next number
  }
  return series; // Return the result
}

Output:

Output

The Fibonacci series can be used in algorithms: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

Explanation: This example highlights the usefulness of the Fibonacci series in algorithm design and problem-solving.

When to Use Fibonacci Series in Dart

Topic Description
Scenario 1 When implementing algorithms that require recursive computations like dynamic programming.
Scenario 2 When performing mathematical operations that involve growth patterns, such as in financial models.
Scenario 3 In graphics programming, where Fibonacci sequences can help create natural-looking designs.

Key Points

Topic Description
Definition The Fibonacci series starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers.
List Usage In Dart, we can use lists to store and manipulate the Fibonacci series.
Dynamic Generation The series can be generated dynamically based on user input.
Algorithm Design Understanding Fibonacci is crucial for designing algorithms in various fields.
Practical Applications Used in areas like data analysis, graphics, and mathematical modeling.
Edge Handling It is important to handle edge cases like zero or negative inputs gracefully.

This tutorial provides a comprehensive understanding of the Fibonacci series in Dart, demonstrating its implementation and practical applications through multiple examples.

Input Required

This code uses input(). Please provide values below: