Wednesday, October 9, 2024
Google search engine
HomeData Modelling & AIFind n-th term in the series 7, 15, 32, …

Find n-th term in the series 7, 15, 32, …

Given a series 7, 15, 32, …… Find the nth term of this series.

Examples : 

Input : 5
Output : 138

Input : 7
Output : 568 
Recommended Practice

Approach: By seeing the pattern of the series we can easily identify that it is a mixed series. 
S = 7, 15, 32….. 
Each element in the series is multiplied by 2 and then incremented one more than the previous. 
S = 7, 15 (2 * 7 + 1), 32 (2 * 15 + 2)…….
By using iteration we can easily find nth term of the series.

Below is the implementation of the above approach :  

C++




// CPP program to find nth term
#include <bits/stdc++.h>
using namespace std;
  
// utility function
int findTerm(int n)
{
    if (n == 1)
        return n;
  
    else {
  
        // since first element of the
        // series is 7, we initialise
        // a variable with 7
        int term = 7;
  
        // Using iteration to find nth 
        // term
        for (int i = 2; i <= n; i++) 
            term = term * 2 + (i - 1);        
  
        return term;
    }
}
  
// driver function
int main()
{
    int n = 5;
    cout << findTerm(n);
    return 0;
}


Java




// Java program to find nth term
import java.lang.*;
  
class GFG {
      
// utility function
static int findTerm(int n)
{
    if (n == 1)
    return n;
  
    else {
  
    // since first element of the
    // series is 7, we initialise
    // a variable with 7
    int term = 7;
  
    // Using iteration to find nth
    // term
    for (int i = 2; i <= n; i++)
        term = term * 2 + (i - 1);
  
    return term;
    }
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 5;
    System.out.print(findTerm(n));
}
}
  
// This code is contributed by Anant Agarwal.


Python3




# Python3 program to find nth term
  
# utility function
def findTerm(n) :
  
    if n == 1 :
        return n
    else :
          
        # since first element of the
        # series is 7, we initialise
        # a variable with 7
        term = 7
  
        # Using iteration to find nth 
        # term
        for i in range(2, n + 1) :
            term = term * 2 + (i - 1);     
  
    return term;
  
# driver function
print (findTerm(5))
  
# This code is contributed by Saloni Gupta


C#




// C# program to find nth term
using System;
  
class GFG {
  
    // utility function
    static int findTerm(int n)
    {
        if (n == 1)
            return n;
  
        else {
  
            // since first element of the
            // series is 7, we initialise
            // a variable with 7
            int term = 7;
  
            // Using iteration to find nth
            // term
            for (int i = 2; i <= n; i++)
                term = term * 2 + (i - 1);
  
            return term;
        }
    }
  
    // Driver code
    public static void Main()
    {
        int n = 5;
        Console.WriteLine(findTerm(n));
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP program to find nth term
  
// utility function
function findTerm($n)
{
    if ($n == 1)
        return $n;
  
    else 
    {
  
        // since first element of the
        // series is 7, we initialise
        // a variable with 7
        $term = 7;
  
        // Using iteration to find nth 
        // term
        for ($i = 2; $i <= $n; $i++) 
            $term = $term * 2 + ($i - 1); 
  
        return $term;
    }
}
  
// Driver Code
$n = 5;
echo(findTerm($n));
  
// This code is contributed by Ajit.
?>


Javascript




<script>
// Javascript program to find nth term
  
// utility function
function findTerm(n)
{
    if (n == 1)
        return n;
  
    else 
    {
  
        // since first element of the
        // series is 7, we initialise
        // a variable with 7
        let term = 7;
  
        // Using iteration to find nth 
        // term
        for (let i = 2; i <= n; i++) 
            term = term * 2 + (i - 1); 
  
        return term;
    }
}
  
// Driver Code
let n = 5;
document.write(findTerm(n));
  
// This code is contributed by gfgking.
</script>


Output

138

Time Complexity: O(n) since for loop will run for n times in worst case
Auxiliary Space: O(1)

If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments