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 sum of 4th power of first n natural numbers. First term is 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 nth term of the series.

Below is the implementation of the above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 *
        
    return ans
   
   
# Driver code
n = 4
print(sumOfSeries(n))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output :

354

Time Complexity : O(n).

Efficient approach :
The pattern in this series is nth term is equal to 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 *
                 + 9 * n * n + n - 1)/ 30
       
# Driver code 
n = 4
print sumOfSeries(n)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

354

Time Complexity : O(1).



My Personal Notes arrow_drop_up


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.



Improved By : vt_m