Program to print the series 2, 15, 41, 80, 132, 197… till N terms

Given a number N, the task is to print the first N terms of the following series: 

2 15 41 80 132 197 275 366 470 587…

Examples:

Input: N = 7

Output: 2 15 41 80 132 197 275

Input: N = 3



Output: 2 15 41

Approach: From the given series we can find the formula for Nth term:

1st term = 2
2nd term = 15 = 13 * 1 + 2
3rd term = 41 = 13 * 2 + 15 = 13 * 3 + 2
4th term = 80 = 13 * 3 + 41 = 13 * 6 + 2
5th term = 132 = 13 * 4 + 80 = 13 * 10 + 2
.
.
Nth term = (13 * N * (N – 1)) / 2 + 2

Therefore:

Nth term of the series

2, 15, 41, 80, 132, 197.... = 13 * (N * (N - 1)) / 2 + 2

Then iterate over numbers in the range [1, N] to find all the terms using the above formula and print them.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to print the 
// given with the given Nth term
  
#include "bits/stdc++.h"
using namespace std;
  
// Function to print the series
void printSeries(int N)
{
  
    int ith_term = 0;
  
    // Generate the ith term and
    for (int i = 1; i <= N; i++) {
  
        ith_term = (13 * i * (i - 1)) / 2 + 2;
        cout << ith_term << ", ";
    }
}
  
// Driver Code
int main()
{
    int N = 7;
  
    printSeries(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to print the 
// given with the given Nth term
import java.util.*;
  
class GFG{
  
// Function to print the series
static void printSeries(int N)
{
    int ith_term = 0;
      
    // Generate the ith term and
    for(int i = 1; i <= N; i++)
    {
       ith_term = (13 * i * (i - 1)) / 2 + 2;
       System.out.print(ith_term + ", ");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 7;
  
    printSeries(N);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to print the
# given with the given Nth term
  
# Function to print the series
def printSeries(N):
      
    ith_term = 0;
  
    # Generate the ith term and
    for i in range(1, N + 1):
        ith_term = (13 * i * (i - 1)) / 2 + 2;
        print(int(ith_term), ", ", end = "");
  
# Driver Code
if __name__ == '__main__':
      
    N = 7;
      
    printSeries(N);
      
# This code is contributed by amal kumar choubey

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to print the 
// given with the given Nth term
using System;
  
class GFG{
  
// Function to print the series
static void printSeries(int N)
{
    int ith_term = 0;
      
    // Generate the ith term and
    for(int i = 1; i <= N; i++)
    {
       ith_term = (13 * i * (i - 1)) / 2 + 2;
       Console.Write(ith_term + ", ");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int N = 7;
  
    printSeries(N);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

2, 15, 41, 80, 132, 197, 275,

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.