Program to print the series 1, 3, 4, 8, 15, 27, 50... till N terms

19 Mar, 2021
Given a number N, the task is to print the first N terms of the following series:
1, 3, 4, 8, 15, 27, 50...
Examples:
Input: N = 7 Output: 1, 3, 4, 8, 15, 27, 50Input: N = 3 Output: 1, 3, 4  
Approach: From the given series we can find the formula for Nth term:
1st term = 1, 2nd term = 3, 3rd term = 4 4th term = 1st term + 2nd term + 3rd term 5th term = 2nd term + 3rd term + 4th term 6th term = 3rd term + 4th term + 5th term . . so on  
Therefore, the idea is to keep track of the last three terms of the series and find the consecutive terms of the series.
Below is the implementation of above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation to print the// N terms of the series whose three// terms are given #include "bits/stdc++.h"using namespace std; // Function to print the seriesvoid printSeries(int n, int a,                 int b, int c){     int d;     // Generate the ith term and    // print it    if (n == 1) {        cout << a << " ";        return;    }    if (n == 2) {        cout << a << " " << b << " ";        return;    }     cout << a << " " << b         << " " << c << " ";     for (int i = 4; i <= n; i++) {        d = a + b + c;        cout << d << " ";        a = b;        b = c;        c = d;    }} // Driver Codeint main(){    int N = 7, a = 1, b = 3;    int c = 4;     // Function Call    printSeries(N, a, b, c);    return 0;}
// Java implementation to print the// N terms of the series whose three// terms are given  //include "bits/stdJava.h"import java.util.*;class GFG{  // Function to print the seriesstatic void printSeries(int n, int a,                         int b, int c){    int d;      // Generate the ith term and    // print it    if (n == 1)    {        System.out.print(a + " ");        return;    }    if (n == 2)    {        System.out.print(a + " " +  b + " ");        return;    }      System.out.print(a + " " +                     b + " " +                      c + " ");      for (int i = 4; i <= n; i++)    {        d = a + b + c;        System.out.print(d + " ");        a = b;        b = c;        c = d;    }}  // Driver Codepublic static void main(String[] args){    int N = 7, a = 1, b = 3;    int c = 4;      // Function Call    printSeries(N, a, b, c);}} // This code is contributed by sapnasingh4991
# Python3 implementation to print the# N terms of the series whose three# terms are given # Function to print the seriesdef printSeries(n, a, b, c):     # Generate the ith term and    # print it    if (n == 1):        print(a, end = " ");        return;         if (n == 2):        print(a, b, end = " ");        return;         print(a, b, c, end = " ");     for i in range (4, n + 1):        d = a + b + c;        print(d, end = " ");        a = b;        b = c;        c = d;     # Driver CodeN = 7; a = 1; b = 3;c = 4; # Function CallprintSeries(N, a, b, c); # This code is contributed by Code_Mech
// C# implementation to print the// N terms of the series whose three// terms are givenusing System;class GFG{ // Function to print the seriesstatic void printSeries(int n, int a,                        int b, int c){    int d;     // Generate the ith term and    // print it    if (n == 1)    {        Console.Write(a + " ");        return;    }    if (n == 2)    {        Console.Write(a + " " +                      b + " ");        return;    }     Console.Write(a + " " +                  b + " " +                  c + " ");     for(int i = 4; i <= n; i++)    {       d = a + b + c;       Console.Write(d + " ");        a = b;       b = c;       c = d;    }} // Driver Codepublic static void Main(){    int N = 7, a = 1, b = 3;    int c = 4;     // Function call    printSeries(N, a, b, c);}} // This code is contributed by rock cool
<script>// javascript implementation to print the// N terms of the series whose three// terms are given // Function to print the seriesfunction printSeries( n,  a,                  b,  c){     let d;     // Generate the ith term and    // print it    if (n == 1) {        document.write( a + " ");        return;    }    if (n == 2) {        document.write( a + " " + b + " ");        return;    }     document.write( a + " " + b         + " " + c + " ");     for (let i = 4; i <= n; i++) {        d = a + b + c;        document.write( d + " ");        a = b;        b = c;        c = d;    }} // Driver Code     let N = 7, a = 1, b = 3;    let c = 4;     // Function Call    printSeries(N, a, b, c);     // This code is contributed by gauravrajput1 </script>
1 3 4 8 15 27 50
 
sapnasingh4991
rock_cool
Code_Mech
GauravRajput1
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Algorithm to solve Rubik's Cube
Minimum number of jumps to reach end
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
The Knight's tour problem | Backtracking-1