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++
// 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 = 4print(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 ?> |
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++
// 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 = 4print 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 ?> |
354
Time Complexity : O(1).
Recommended Posts:
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- N-th term in the series 1, 11, 55, 239, 991,....
- n-th term in series 2, 12, 36, 80, 150....
- Sum of first n term of Series 3, 5, 9, 17, 33....
- Find n-th term in series 1 2 2 3 3 3 4 4 4 4....
- Find n-th term of series 1, 3, 6, 10, 15, 21...
- Find Nth term of the series 0, 6, 0, 12, 0, 90...
- Find Nth term of the series 1, 6, 18, 40, 75, ....
- Find n-th term in the series 9, 33, 73,129 ...
- Find Nth term of the series 1, 5, 32, 288 ...
- Find n-th term in the series 7, 15, 32, ...
- Finding n-th term of series 3, 13, 42, 108, 235…
- Find nth term of the series 5 2 13 41
- Find n-th term of series 1, 4, 27, 16, 125, 36, 343 .......
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



