🐍 Fibonacci Series in Python — Recursion & Loop Explained

Fibonacci Series in Java, Python, and JavaScript | Simple Guide


🐍 Fibonacci Series in Python — Recursion & Loop Explained

📘 Introduction

The Fibonacci series is one of the most classic problems in programming. It’s a great way to understand recursion, loops, and sequence generation. In this post, we’ll explore how to generate the Fibonacci series in Java, Python, and JavaScript — using both recursive and non-recursive methods.

🔢 What is the Fibonacci Series?

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding numbers. The sequence starts with:

0, 1, 1, 2, 3, 5, 8, 13, 21, ...

✅ Solution #1: Using Recursion in Python

Recursion is a function calling itself — it's a great way to solve problems that break down into smaller, repeating subproblems.

def fibonacci_recursive(n):
    if n <= 0:
        return []
    elif n == 1:
        return [0]
    elif n == 2:
        return [0, 1]
    else:
        series = fibonacci_recursive(n - 1)
        series.append(series[-1] + series[-2])
        return series

# Example
n = int(input("Enter number of terms: "))
print(fibonacci_recursive(n))

🧾 Sample Output:

Enter number of terms: 10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

✅ Solution #2: Without Recursion (Using Loop)

Loops are more efficient and easier to debug, especially for large values.

def fibonacci_loop(n):
    if n <= 0:
        return []
    elif n == 1:
        return [0]
    
    fib = [0, 1]
    for _ in range(2, n):
        fib.append(fib[-1] + fib[-2])
    return fib

# Example
n = int(input("Enter number of terms: "))
print(fibonacci_loop(n))

🧾 Sample Output:

Enter number of terms: 10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

🧠 When to Use What?

  • Use recursion if you want to learn or showcase elegant solutions.

  • Use loops when performance and efficiency matter.

Whether you're building logic for fun, a school project, or interview prep, Fibonacci in Python is a smart way to get hands-on with the basics.


🌐 Fibonacci Series in JavaScript — Learn With Recursion & Loops

If you're working with JavaScript and want to sharpen your algorithmic thinking, the Fibonacci series is a great place to start. It’s a simple yet powerful way to learn recursion and iteration.

🔁 What is the Fibonacci Series?

Just like in Python, the Fibonacci sequence follows a simple rule:

Each number is the sum of the two numbers before it.

It starts like:

0, 1, 1, 2, 3, 5, 8, 13, ...

✅ Solution #1: Using Recursion in JavaScript

Here's a basic recursive function to return the Fibonacci series:

function fibonacciRecursive(n) {
  if (n <= 0) return [];
  if (n === 1) return [0];
  if (n === 2) return [0, 1];

  const series = fibonacciRecursive(n - 1);
  series.push(series[series.length - 1] + series[series.length - 2]);
  return series;
}

// Example usage:
let n = prompt("Enter number of terms:");
console.log(fibonacciRecursive(Number(n)));

🧾 Sample Output in Console:

Enter number of terms: 8
[0, 1, 1, 2, 3, 5, 8, 13]

✅ Solution #2: Without Recursion (Using a Loop)

Loops in JavaScript are clean and efficient:

function fibonacciLoop(n) {
  if (n <= 0) return [];
  if (n === 1) return [0];

  const fib = [0, 1];
  for (let i = 2; i < n; i++) {
    fib.push(fib[i - 1] + fib[i - 2]);
  }
  return fib;
}

// Example usage:
let n = prompt("Enter number of terms:");
console.log(fibonacciLoop(Number(n)));

🧾 Sample Output:

Enter number of terms: 10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

🤔 Final Thoughts

The Fibonacci series may seem simple, but it teaches core programming concepts like:

  • Recursion

  • Loops

  • Time complexity

  • Base case logic

Whether you're coding for fun, learning fundamentals, or preparing for interviews — this sequence will keep popping up.

Happy coding! ✨

Post a Comment

0 Comments