给定整数N ,任务是找到以下系列的第N个项:
0, 8, 64, 216, 512, 1000, 1728, . . .
例子:
Input: N = 6
Output: 1000
Input: N = 5
Output: 512
方法:
- 给定系列0、8、64、216、512、1000、1728,…也可以写成0 *(0 2 ),2 *(2 2 ),4 *(4 2 ),6 *(6 2 ), 8 *(8 2 ),10 *(10 2 ),…
- 观察到0,2,4,6,10,…是在AP和该序列的第n项可使用下式术语中找到= 1 +(N – 1)* d,其中a 1是第一项,正是位置项, d是共同的区别。
- 要获得原始系列中的术语, term =术语*(term 2 ),即term 3 。
- 最后打印该术语。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the nth term of the given series
long term(int n)
{
// Common difference
int d = 2;
// First term
int a1 = 0;
// nth term
int An = a1 + (n - 1) * d;
// nth term of the given series
An = pow(An, 3);
return An;
}
// Driver code
int main()
{
int n = 5;
cout << term(n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
public class GFG {
// Function to return the nth term of the given series
static int nthTerm(int n)
{
// Common difference and first term
int d = 2, a1 = 0;
// nth term
int An = a1 + (n - 1) * d;
// nth term of the given series
return (int)Math.pow(An, 3);
}
// Driver code
public static void main(String[] args)
{
int n = 5;
System.out.println(nthTerm(n));
}
}
Python3
# Python3 implementation of the approach
# Function to return the nth term of the given series
def term(n):
# Common difference
d = 2
# First term
a1 = 0
# nth term
An = a1 +(n-1)*d
# nth term of the given series
An = An**3
return An;
# Driver code
n = 5
print(term(n))
C#
// C# implementation of the approach
using System;
public class GFG {
// Function to return the nth term of the given series
static int nthTerm(int n)
{
// Common difference and first term
int d = 2, a1 = 0;
// nth term
int An = a1 + (n - 1) * d;
// nth term of the given series
return (int)Math.Pow(An, 3);
}
// Driver code
public static void Main()
{
int n = 5;
Console. WriteLine(nthTerm(n));
}
}
// This code is contributed by Mutual singh.
PHP
Javascript
输出:
512