Open In App

Find n-th term of series 1, 3, 6, 10, 15, 21...

Last Updated : 21 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number n, find the n-th term in the series 1, 3, 6, 10, 15, 21...

Examples

Input 3
Output 6

Input 4
Output 10

The given series represent triangular numbers which are sums of natural numbers.

[Naive approach] Using Loop - O(n) time and O(1) space

The series sums the first n natural numbers, with each term adding one more number. The nth term is the sum of the first n natural numbers.

C++
#include <iostream>
using namespace std;

// Function to find the nth term of series
int term(int n)
{      
    // Loop to add numbers
    int ans = 0;
    for (int i = 1; i <= n; i++)    
        ans += i; 
    
    return ans;
}

int main()
{
    int n = 4;
    cout << term(n) ;
    return 0;
}
C
#include <stdio.h>

// Function to find the nth term of series
int term(int n)
{      
    // Loop to add numbers
    int ans = 0;
    for (int i = 1; i <= n; i++)    
        ans += i; 
    
    return ans;
}


int main()
{
    int n = 4;
    printf("%d", term(n));
    return 0;
}
Java
import java.io.*;

class GfG {
    
    // Function to find the nth term of series
    static int term(int n)
    {     
        // Loop to add numbers
        int ans = 0;
        for (int i = 1; i <= n; i++) 
            ans += i; 
        
        return ans;
    }

    
    public static void main(String args[])
    {
        int n = 4;
        System.out.println(term(n));
    }
}
Python
# Function to find the
# nth term of series
def term(n) :
    # Loop to add numbers
    ans = 0
    for i in range(1,n+1) :
        ans = ans + i 
     
    return ans


n = 4
print(term(n))
C#
using System;

class GfG {

    // Function to find the nth term
    // of series
    static int term(int n)
    {
        
        // Loop to add numbers
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans += i;

        return ans;
    }

    public static void Main()
    {
        int n = 4;
        
        Console.WriteLine(term(n));
    }
}
JavaScript
// Function to find the nth term of series
function term(n)
{ 
    
    // Loop to add numbers
    let ans = 0;
    for(let i = 1; i <= n; i++)    
        ans += i; 
    
    return ans;
}

let n = 4;
console.log(term(n));

Output
10

[Expected Approach] Using Formula - O(1) time and O(1) space

The pattern in this series is nth term is equal to sum of (n-1)th term and n.

n = 2
2nd term equals to sum of 1st term and 2 i.e
A2 = A1 + 2 = 1 + 2 = 3
Similarly,
A3 = A2 + 3 = 3 + 3 = 6 and so on..

We get: 

A(n) = A(n - 1) + n
= A(n - 2) + n + (n - 1)
= A(n - 3) + n + (n - 1) + (n - 2)
.
.
.
= A(1) + 2 + 3... + (n-1) + n

A(n) = 1 + 2 + 3 + 4... + (n - 1) + n = n(n + 1) / 2
i.e A(n) is sum of First n natural numbers.

C++
#include <bits/stdc++.h>
using namespace std;

// Function to find nth term
int term(int n)
{
    return n * (n + 1) / 2;
}


int main()
{
    int n = 4;
    cout << term(n);
    return 0;
}
C
#include <stdio.h>

// Function to find nth term
int term(int n)
{
    return n * (n + 1) / 2;
}


int main()
{
    int n = 4;
    printf("%d", term(n));
    return 0;
}
Java
import java.io.*;

class GfG{
    
    // Function to find nth term
    static int term(int n){
        return n * (n + 1) / 2;
    }
    
    public static void main (String[] args) {
        int n = 4;
        System.out.println(term(n));
    }
}
Python
# Function to print nth term 
# of series 1 3 6 10 .... 
def term(n):
    return n *(n + 1) / 2
    
n = 4
print term(n)
C#
using System;

class GfG {

    // Function to find nth term
    static int term(int n)
    {
        return n * (n + 1) / 2;
    }

    // Driver Code
    public static void Main()
    {
        int n = 4;
        
        Console.WriteLine(term(n));
    }
}
JavaScript
// Function to find nth term
function term(n)
{
    return parseInt(n * (n + 1) / 2);
}

let n = 4;
console.log(term(n));

Output
10

Practice Tags :

Similar Reads