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

07 Jun, 2022
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 : The series basically represents sums of natural numbers. First term is sum of single number. Second term is sum of two numbers, and so on. A simple solution is to add the first n natural numbers. 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find n-th term of// series 1, 3, 6, 10, 15, 21...#include <iostream>using namespace std; // Function to find the nth term of seriesint term(int n){         // Loop to add numbers    int ans = 0;    for (int i = 1; i <= n; i++)           ans += i;         return ans;} // Driver codeint main(){    int n = 4;    cout << term(n) ;    return 0;}
// Java program to find n-th term of// series 1, 3, 6, 10, 15, 21...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;    }          // Driver code    public static void main(String args[])    {        int n = 4;        System.out.println(term(n));    }}  // This code is contributed by Nikita Tiwari.
# Python 3 program to find# n-th term of# series 1, 3, 6, 10, 15, 21...      # Function to find the# nth term of seriesdef term(n) :    # Loop to add numbers    ans = 0    for i in range(1,n+1) :        ans = ans + i          return ans  # Driver coden = 4print(term(n)) # This code is contributed# by Nikita Tiwari.
// C# program to find n-th term of// series 1, 3, 6, 10, 15, 21...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;    }     // Driver code    public static void Main()    {        int n = 4;                 Console.WriteLine(term(n));    }} // This code is contributed by vt_m.
<?php// PHP program to find n-th term of// series 1, 3, 6, 10, 15, 21... // Function to find the nth// term of seriesfunction term($n){         // Loop to add numbers    $ans = 0;    for ($i = 1; $i <= $n; $i++)        $ans += $i;         return $ans;} // Driver code$n = 4;echo(term($n)) ; // This code is contributed by Ajit.?>
<script> // Javascript program to find n-th term of// series 1, 3, 6, 10, 15, 21... // Function to find the nth term of seriesfunction term(n){         // Loop to add numbers    let ans = 0;    for(let i = 1; i <= n; i++)           ans += i;         return ans;} // Driver codelet n = 4; document.write(term(n)); // This code is contributed by rishavmahato348 </script>
Output: 
10
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
The pattern in this series is nth term is equal to sum of (n-1)th term and n.Example : 
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.
Below is the implementation of the above approach: 
C++
Java
Python
C#
PHP
Javascript
// CPP program to find the n-th// term in series 1 3 6 10 ...#include <bits/stdc++.h>using namespace std; // Function to find nth termint term(int n){    return n * (n + 1) / 2;} // Driver codeint main(){    int n = 4;    cout << term(n);    return 0;}
// Java program to find the n-th// term in series 1 3 6 10 ...import java.io.*; class Series {         // Function to find nth term    static int term(int n){        return n * (n + 1) / 2;    }         // Driver Code    public static void main (String[] args) {        int n = 4;        System.out.println(term(n));    }} // This code is contributed by Chinmoy Lenka
# Python program to find the Nth# term in series 1 3 6 10 ... # Function to print nth term# of series 1 3 6 10 ....def term(n):    return n *(n + 1) / 2     # Driver coden = 4print term(n)
// C# program to find the n-th// term in series 1 3 6 10 ...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));    }} // This code is contributed by vt_m.
<?php// PHP program to find the n-th// term in series 1 3 6 10 ... // Function to find nth termfunction term($n){    return $n * ($n + 1) / 2;} // Driver code$n = 4;echo(term($n)); // This code is contributed by Ajit.?>
<script>// Javascript program to find the n-th// term in series 1 3 6 10 ... // Function to find nth termfunction term(n){    return parseInt(n * (n + 1) / 2);} // Driver codelet n = 4;document.write(term(n)); // This code is contributed by subhammahato348.</script>
Output :  
10
Time Complexity: O(1), as we are not using any loop or recursion to traverse.
Auxiliary Space: O(1), as we are not using any extra space. 
jit_t
rishavmahato348
subhammahato348
rohitsingh57
series
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operators in C / C++
Modular multiplicative inverse
Find cubic root of a number
Modulo Operator (%) in C/C++ with Examples
Find first and last digits of a number
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Interfaces in Java
Inheritance in C++