Open In App

Find n terms of Fibonacci type series with given first two terms

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given first two numbers of series, find n terms of series with these two numbers. The given series follows the same concept as Fibonacci series, i.e., n-th term is sum of (n-1)-th and (n-2)-th terms.

Examples: 

Input: first = 5, sec = 8, n = 5
Output: 5, 8, 13, 21, 34

Input: first = 2, sec = 4, n = 5
Output: 2, 4, 6, 10, 16

Approach: 
The approach is similar to finding Fibonacci series where the summation of last two terms form the next term. Find the sum of first two given numbers, second number now will serve as first number to find the next term and the sum produced will serve as second number to find the next term. Sum of these two newly formed first and second term will form the next required term.

Below is the implementation program:

C++




// C++ program to find n terms of
// series from given two numbers
#include <bits/stdc++.h>
using namespace std;
 
// Function to find n terms
// of series
void findSeries(int num, int first, int sec)
{
 
    cout << first << " " << sec << " ";
    int counter = 0, sum;
 
    // find next (num - 2) terms of series
    // as first two terms are already given
    while (counter < num - 2) {
        sum = first + sec;
        cout << sum << " ";
        first = sec;
        sec = sum;
        counter++;
    }
}
 
// Drivers code
int main()
{
 
    int n = 5, first = 2, sec = 4;
    findSeries(n, first, sec);
 
    return 0;
}


Java




// Java program to find n terms of
// series from given two numbers
import java.io.*;
 
class GFG {
 
    // Function to find n terms
    // of series
    static void findSeries(int num,
                 int first, int sec)
    {
     
        System.out.print(first + " "
                       + sec + " ");
        int counter = 0, sum;
     
        // find next (num - 2) terms
        // of series as first two
        // terms are already given
        while (counter < num - 2)
        {
            sum = first + sec;
            System.out.print( sum + " ");
            first = sec;
            sec = sum;
            counter++;
        }
    }
     
    // Drivers code
    public static void main (String[] args)
    {
        int n = 5, first = 2, sec = 4;
         
        findSeries(n, first, sec);
    }
}
 
// This code is contributed by vt_m.


Python3




# Python3 program to find n terms of
# series from given two numbers
 
# Function to find n terms
# of series
def findSeries(num, first, sec) :
    print ("{} {} ".format(first, sec),
                               end="")
    counter = 0
 
    # find next (num - 2) terms of
    # series as first two terms are
    # already given
    while (counter < num - 2):
        sum = first + sec
        print ("{} ".format(sum), end="")
        first = sec
        sec = sum
        counter = counter + 1
 
# Drivers code
n = 5
first = 2
sec = 4
findSeries(n, first, sec)
 
# This code is contributed by
# Manish Shaw (manishshaw1)


C#




// C# program to find n terms of
// series from given two numbers
using System;
 
class GFG {
 
    // Function to find n terms
    // of series
    static void findSeries(int num,
                int first, int sec)
    {
     
        Console.Write(first + " "
                    + sec + " ");
        int counter = 0, sum;
     
        // find next (num - 2) terms
        // of series as first two
        // terms are already given
        while (counter < num - 2)
        {
            sum = first + sec;
            Console.Write( sum + " ");
            first = sec;
            sec = sum;
            counter++;
        }
    }
     
    // Drivers code
    public static void Main ()
    {
        int n = 5, first = 2, sec = 4;
         
        findSeries(n, first, sec);
    }
}
 
// This code is contributed by anuj_67.


PHP




<?php
// PHP program to find n terms of
// series from given two numbers
 
// Function to find n terms
// of series
function findSeries( $num, $first, $sec)
{
 
    echo $first , " " , $sec , " ";
    $counter = 0; $sum;
 
    // find next (num - 2) terms of series
    // as first two terms are already given
    while ($counter < $num - 2)
    {
        $sum = $first + $sec;
        echo $sum , " ";
        $first = $sec;
        $sec = $sum;
        $counter++;
    }
}
 
    // Driver Code
    $n = 5;
    $first = 2;
    $sec = 4;
    findSeries($n, $first, $sec);
 
// This code is contributed by anuj_67.
?>


Javascript




<script>
// javascript program to find n terms of
// series from given two numbers
 
// Function to find n terms
// of series
function findSeries( num,  first,  sec)
{
    document.write(first + " " + sec + " ");
    let counter = 0, sum;
 
    // find next (num - 2) terms of series
    // as first two terms are already given
    while (counter < num - 2)
    {
        sum = first + sec;
        document.write(sum + " ");
        first = sec;
        sec = sum;
        counter++;
    }
}
 
// Drivers code
    let n = 5, first = 2, sec = 4;
    findSeries(n, first, sec);
     
// This code is contributed by Rajput-Ji
 
</script>


Output

2 4 6 10 16 

Time Complexity: O(n), where N represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Method: list comprehension

  1. Define a function that takes three arguments: first, sec, and n. These represent the first two terms of the series and the number of terms to generate, respectively.
  2. Create a list called series containing the first two terms of the series (first and sec).
  3. Use a list comprehension to generate the (n-2) subsequent terms of the series. To do this, specify the elements of the list within square brackets, followed by a for loop that generates those elements. The loop should start at the third term, or index 2, since we already have the first two terms in the list. Within the loop, use the formula for generating the next term of the series: the sum of the (n-1)th and (n-2)th terms. Append each subsequent term to the series list.
  4. Return the series list containing the desired Fibonacci-type series.

C++




#include <iostream>
#include <vector>
using namespace std;
 
// Function to generate a Fibonacci type series
vector<int> fibonacci_type_series(int first, int sec, int n) {
    vector<int> series = {first, sec};
    for (int i = 2; i < n; i++)
        series.push_back(series[i-1] + series[i-2]);
    return series;
}
 
// Example usage
int main() {
    vector<int> series = fibonacci_type_series(5, 8, 5);
    for (int i = 0; i < series.size(); i++)
        cout << series[i] << " "; // Output: 5 8 13 21 34
    return 0;
}


Java




// Java program for the above approach
import java.util.ArrayList;
import java.util.List;
 
public class FibonacciTypeSeries {
 
    // Function to generate a Fibonacci
    // type series
    public static List<Integer>
    fibonacciTypeSeries(int first, int sec, int n)
    {
        List<Integer> series = new ArrayList<>();
        series.add(first);
        series.add(sec);
        for (int i = 2; i < n; i++)
            series.add(series.get(i - 1)
                       + series.get(i - 2));
        return series;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        List<Integer> series = fibonacciTypeSeries(5, 8, 5);
        for (int i = 0; i < series.size(); i++)
            System.out.print(series.get(i) + " ");
    }
}


Python3




def fibonacci_type_series(first, sec, n):
    series = [first, sec]
    [series.append(series[i-1] + series[i-2]) for i in range(2, n)]
    return series
 
# Example usage
series = fibonacci_type_series(5, 8, 5)
print(series) # Output: [5, 8, 13, 21, 34]


C#




using System;
using System.Collections.Generic;
 
public class Program {
    // Function to generate a Fibonacci type series
    public static List<int> FibonacciTypeSeries(int first, int sec, int n) {
        List<int> series = new List<int> { first, sec };
        for (int i = 2; i < n; i++) {
            series.Add(series[i-1] + series[i-2]);
        }
        return series;
    }
 
    // Example usage
    public static void Main() {
        List<int> series = FibonacciTypeSeries(5, 8, 5);
        for (int i = 0; i < series.Count; i++) {
            Console.Write(series[i] + " "); // Output: 5 8 13 21 34
        }
    }
}


Javascript




function fibonacci_type_series(first, sec, n) {
    let series = [first, sec];
    for (let i = 2; i < n; i++)
        series.push(series[i-1] + series[i-2]);
    return series;
}
 
// Example usage
let series = fibonacci_type_series(5, 8, 5);
console.log(series); // Output: [5, 8, 13, 21, 34]


Output

[5, 8, 13, 21, 34]

The time complexity is O(n)

The auxiliary space is O(n)



Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads