Last Updated: 21 Jan, 2023

Print Fibonacci Series

Easy
Asked in companies
ToXSL Technologies Pvt LtdNewgen SoftwareConsuma

Problem statement

Given an integer ‘n’, return first n Fibonacci numbers using a generator function.


Example:
Input: ‘n’ = 5

Output: 0 1 1 2 3

Explanation: First 5 Fibonacci numbers are: 0, 1, 1, 2, and 3.
Note:
You don't need to print anything. Just implement the given function.
Input Format
The first and only input line contains one integer, n, the required number of Fibonacci numbers to return.
Output format:
Return first n Fibonacci numbers using a generator function.

Approaches

01 Approach

Create a generator function ‘generatorFunction’, which takes the number of Fibonacci numbers to return (say this is ‘n’). In this function, create a variable ‘count’ initially assigned to one. Create two more variables, ‘a’ and ‘b’, each assigned to 0 and 1, respectively. Now, use a while loop on ‘count’ variable from ‘1’ to’ ‘n’. In each iteration, first, yield ‘a’ and then assign ‘b’ to ‘a’ and ‘a + b’ to ‘b’.

Now, create an empty list, iterate over the ‘generator’ object and insert the items in the list. And Finally, return the list.
 

The steps are as follows:-
 

// Generator function to generate Fibonacci numbers

function generatorFunction(int n):

  1. Initialize a variable ‘count’, which is initially assigned to 1.
  2. Initialize two more variables, ‘a’ and ‘b’, each assigned to 0 and 1, respectively.
  3. While count <= n:
    • yield a.
    • Assign ‘b’ to ‘a’ and ‘a + b’ to ‘b’.
    • Increment count.

 

// Function to generate first n Fibonacci numbers

function generateFibonacciNumbers(int n):

  1. Create an empty list, ‘listOfIntegers’, which stores the Fibonacci Numbers.
  2. Iterate over each item produced by generatorFunction(n):
    • Insert the value in the list ‘listOfIntegers’.
  3. Return ‘listOfIntegers’.

02 Approach

To calculate the nth Fibonacci number efficiently, we can use memoization. Memoization is a technique of storing previously computed values to avoid redundant calculations.

Algorithm:

Create a function fibMemoization(n, memo) that takes an integer n as input and a vector memo to store the computed Fibonacci values.

Inside the fibMemoization function:

Implement base cases:

  • If n is 0, return 0, as the 0th Fibonacci number is 0.
  • If n is 1, return 1, as the 1st Fibonacci number is 1.

If the Fibonacci value for n is already computed (stored in memo[n]), return it.

Otherwise, calculate the Fibonacci value for n using recursion:

  • Calculate fibMemoization(n - 1, memo) to get the (n - 1)th Fibonacci number.
  • Calculate fibMemoization(n - 2, memo) to get the (n - 2)th Fibonacci number.
  • Add these two values to get the nth Fibonacci number and store it in memo[n].

Return the computed Fibonacci value for n.

Create a function getFibonacciNumber(n) that takes an integer n as input:

  • Initialize a memoization table memo of size n + 1 with all values set to -1.
  • Call the fibMemoization(n, memo) function to calculate the nth Fibonacci number using memoization.
  • Return the computed nth Fibonacci number.