Saturday, January 11, 2025
Google search engine
HomeData Modelling & AIn-th term of series 1, 17, 98, 354……

n-th term of series 1, 17, 98, 354……

Given a series 1, 17, 98, 354 …… Find the nth term of this series.
The series basically represents the sum of the 4th power of first n natural numbers. First-term is the sum of 14. Second term is sum of two numbers i.e (14 + 24 = 17), third term i.e.(14 + 24 + 34 = 98) and so on.

Examples:  

Input : 5
Output : 979

Input : 7
Output : 4676 

Naive Approach : 
A simple solution is to add the 4th powers of first n natural numbers. By using iteration we can easily find the nth term of the series.
Below is the implementation of the above approach : 

C++




// CPP program to find n-th term of
// series
#include <iostream>
using namespace std;
 
// Function to find the nth term of series
int sumOfSeries(int n)
{
    // Loop to add 4th powers
    int ans = 0;
    for (int i = 1; i <= n; i++)
        ans += i * i * i * i;
 
    return ans;
}
 
// Driver code
int main()
{
    int n = 4;
    cout << sumOfSeries(n);
    return 0;
}


Java




// Java program to find n-th term of
// series
import java.io.*;
 
class GFG {
 
    // Function to find the nth term of series
    static int sumOfSeries(int n)
    {
        // Loop to add 4th powers
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans += i * i * i * i;
 
        return ans;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 4;
        System.out.println(sumOfSeries(n));
    }
}


Python3




# Python 3 program to find
# n-th term of
# series
  
      
# Function to find the
# nth term of series
def sumOfSeries(n) :
    # Loop to add 4th powers
    ans = 0
    for i in range(1, n + 1) :
        ans = ans + i * i * i * i
       
    return ans
  
  
# Driver code
n = 4
print(sumOfSeries(n))


C#




// C# program to find n-th term of
// series
using System;
class GFG {
 
    // Function to find the
    // nth term of series
    static int sumOfSeries(int n)
    {
         
        // Loop to add 4th powers
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans += i * i * i * i;
 
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(sumOfSeries(n));
    }
}
 
// This code is contributed by anuj_67


PHP




<?php
// PHP program to find
// n-th term of series
 
// Function to find the
// nth term of series
function sumOfSeries( $n)
{
    // Loop to add 4th powers
    $ans = 0;
    for ( $i = 1; $i <= $n; $i++)
        $ans += $i * $i * $i * $i;
 
    return $ans;
}
 
// Driver code
$n = 4;
echo sumOfSeries($n);
 
// This code is contributed
// by anuj_67
?>


Javascript




<script>
 
// Javascript program to find n-th term of
// series
 
 
// Function to find the nth term of series
function sumOfSeries( n)
{
    // Loop to add 4th powers
    let ans = 0;
    for (let i = 1; i <= n; i++)
        ans += i * i * i * i;
 
    return ans;
}
 
 
// Driver Code
 
let n = 4;
document.write(sumOfSeries(n));
 
 
</script>


Output : 

354

 

Time Complexity : O(n).

Auxiliary Space: O(1)

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

Examples: 

n = 2
2nd term equals to sum of 1st term and 24 i.e 16
A2 = A1 + 16 
   = 1 + 16
   = 17

Similarly,
A3 = A2 + 34
   = 17 + 81
   = 98 and so on..

We get: 

A(n) = A(n - 1) + n4 
     = A(n - 2) + n4  + (n-1)4 
     = A(n - 3) + n4  + (n-1)4 + (n-2)4
       .
       .
       .
     = A(1) + 16 + 81... + (n-1)4 + n4

A(n) = 1 + 16 + 81 +... + (n-1)4 + n4
     = n(n + 1)(6n3 + 9n2 + n - 1) / 30 

i.e A(n) is sum of 4th powers of First n natural numbers.

Below is the implementation of the above approach: 

C++




// CPP program to find the n-th
// term in series
#include <bits/stdc++.h>
using namespace std;
 
// Function to find nth term
int sumOfSeries(int n)
{
    return n * (n + 1) * (6 * n * n * n
                 + 9 * n * n + n - 1) / 30;
}
 
// Driver code
int main()
{
    int n = 4;
    cout << sumOfSeries(n);
    return 0;
}


Java




// Java program to find the n-th
// term in series
import java.io.*;
 
class Series {
 
    // Function to find nth term
    static int sumOfSeries(int n)
    {
        return n * (n + 1) * (6 * n * n * n
                    + 9 * n * n + n - 1) / 30;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(sumOfSeries(n));
    }
}


Python




# Python program to find the Nth
# term in series
  
# Function to print nth term
# of series
def sumOfSeries(n):
    return n * (n + 1) * (6 * n * n * n
                 + 9 * n * n + n - 1)/ 30
      
# Driver code
n = 4
print sumOfSeries(n)


C#




// C# program to find the n-th
// term in series
using System;
class Series {
 
    // Function to find nth term
    static int sumOfSeries(int n)
    {
        return n * (n + 1) * (6 * n * n * n
                  + 9 * n * n + n - 1) / 30;
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine(sumOfSeries(n));
    }
}
 
// This code is contributed by anuj_67


PHP




<?php
// PHP program to find the n-th
// term in series
 
// Function to find nth term
function sumOfSeries( $n)
{
    return $n * ($n + 1) * (6 * $n * $n *
           $n + 9 * $n * $n + $n - 1) / 30;
}
 
    // Driver code
    $n = 4;
    echo sumOfSeries($n);
 
// This code is contributed by anuj_67
?>


Javascript




<script>
    // Javascript program to find the n-th term in series
     
    // Function to find nth term
    function sumOfSeries(n)
    {
        return n * (n + 1) * (6 * n * n * n + 9 * n * n + n - 1) / 30;
    }
     
    let n = 4;
      document.write(sumOfSeries(n));
 
// This code is contributed by divyeshrabadiya07.
</script>


Output: 

354

 

Time Complexity : O(1).

Auxiliary Space: O(1)
 

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