给出一个系列1,17,98,354……找出这个系列的第n项。 级数基本上代表前n个自然数的四次方之和。第一项是1的和 4. .第二项是两个数字的总和,即(1 4. + 2 4. =17),第三项,即(1) 4. + 2 4. + 3 4. )等等。
null
例如:
Input : 5Output : 979Input : 7Output : 4676
天真的方法: 一个简单的解决方案是将前n个自然数的四次方相加。通过迭代,我们可以很容易地找到级数的第n项。 以下是上述方法的实施情况:
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> |
输出:
354
时间复杂性: O(n)。
有效方法: 本系列中的模式是n项等于 第(n-1)项与第n项之和 4. .
例如:
n = 22nd term equals to sum of 1st term and 24 i.e 16A2 = A1 + 16 = 1 + 16 = 17Similarly,A3 = A2 + 34 = 17 + 81 = 98 and so on..
我们得到:
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 + n4A(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.
以下是上述方法的实施情况:
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> |
输出:
354
时间复杂性: O(1)。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END