给定数字N ,任务是找到级数的第N个项,其中每个项交替相差6和2。
例子:
Input: N = 6
Output: 24
Explanation:
The Nth term is 0 + 6 + 2 + 6 + 2 + 6 + 2 = 24
Input: N = 3
Output: 14
Explanation:
The Nth term is 0 + 6 + 2 + 6 = 14
天真的方法:这个想法是从1开始以6和2的增量迭代,直到达到第N个项。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find Nth term
void findNthTerm(int N)
{
int ans = 0;
// Iterate from 1 till Nth term
for (int i = 0; i < N; i++) {
// Check if i is even and
// then add 6
if (i % 2 == 0) {
ans = ans + 6;
}
// Else add 2
else {
ans = ans + 2;
}
}
// Print ans
cout << ans << endl;
}
// Driver Code
int main()
{
int N = 3;
findNthTerm(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find Nth term
static void findNthTerm(int N)
{
int ans = 0;
// Iterate from 1 till Nth term
for (int i = 0; i < N; i++) {
// Check if i is even and
// then add 6
if (i % 2 == 0) {
ans = ans + 6;
}
// Else add 2
else {
ans = ans + 2;
}
}
// Print ans
System.out.print(ans +"\n");
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
findNthTerm(N);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program for the above approach
# Function to find Nth term
def findNthTerm(N):
ans = 0
# Iterate from 1 till Nth term
for i in range(N):
# Check if i is even and
# then add 6
if (i % 2 == 0) :
ans = ans + 6
# Else add 2
else :
ans = ans + 2
# Print ans
print(ans)
# Driver Code
if __name__=='__main__':
N = 3
findNthTerm(N)
# This code is contributed by AbhiThakur
C#
// C# program for the above approach
using System;
class GFG{
// Function to find Nth term
static void findNthTerm(int N)
{
int ans = 0;
// Iterate from 1 till Nth term
for (int i = 0; i < N; i++) {
// Check if i is even and
// then add 6
if (i % 2 == 0) {
ans = ans + 6;
}
// Else add 2
else {
ans = ans + 2;
}
}
// Print ans
Console.Write(ans +"\n");
}
// Driver Code
public static void Main()
{
int N = 3;
findNthTerm(N);
}
}
// This code is contributed by AbhiThakur
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find Nth term
void findNthTerm(int N)
{
int ans;
// Check if N is even
if (N % 2 == 0) {
// Formula for n is even
ans = (N / 2) * 6
+ (N / 2) * 2;
}
// Check if N is odd
else {
// Formula for N is odd
ans = (N / 2 + 1) * 6
+ (N / 2) * 2;
}
// Print ans
cout << ans << endl;
}
// Driver Code
int main()
{
int N = 3;
findNthTerm(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find Nth term
static void findNthTerm(int N)
{
int ans;
// Check if N is even
if (N % 2 == 0) {
// Formula for n is even
ans = (N / 2) * 6
+ (N / 2) * 2;
}
// Check if N is odd
else {
// Formula for N is odd
ans = (N / 2 + 1) * 6
+ (N / 2) * 2;
}
// Print ans
System.out.print(ans +"\n");
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
findNthTerm(N);
}
}
// This code contributed by PrinciRaj1992
Python3
# Python3 program for the above approach
# Function to find Nth term
def findNthTerm(N):
ans = 0;
# Check if N is even
if (N % 2 == 0):
# Formula for n is even
ans = (N // 2) * 6 + (N // 2) * 2;
# Check if N is odd
else:
# Formula for N is odd
ans = (N // 2 + 1) * 6 + (N // 2) * 2;
# Print ans
print(ans);
# Driver Code
if __name__ == '__main__':
N = 3;
findNthTerm(N);
# This code is contributed by Rajput-Ji
C#
// C# program for the above approach
using System;
class GFG{
// Function to find Nth term
static void findNthTerm(int N)
{
int ans;
// Check if N is even
if (N % 2 == 0) {
// Formula for n is even
ans = (N / 2) * 6
+ (N / 2) * 2;
}
// Check if N is odd
else {
// Formula for N is odd
ans = (N / 2 + 1) * 6
+ (N / 2) * 2;
}
// Print ans
Console.Write(ans +"\n");
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
findNthTerm(N);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
14
时间复杂度: O(N)
高效的方法:我们可以使用以下公式找到第N个项:
- 如果N为奇数:第N个项由(N / 2 +1)* 6 +(N / 2)* 2给出。
- 如果N为偶数:第N个项由(N / 2)* 6 +(N / 2)* 2给出。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find Nth term
void findNthTerm(int N)
{
int ans;
// Check if N is even
if (N % 2 == 0) {
// Formula for n is even
ans = (N / 2) * 6
+ (N / 2) * 2;
}
// Check if N is odd
else {
// Formula for N is odd
ans = (N / 2 + 1) * 6
+ (N / 2) * 2;
}
// Print ans
cout << ans << endl;
}
// Driver Code
int main()
{
int N = 3;
findNthTerm(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find Nth term
static void findNthTerm(int N)
{
int ans;
// Check if N is even
if (N % 2 == 0) {
// Formula for n is even
ans = (N / 2) * 6
+ (N / 2) * 2;
}
// Check if N is odd
else {
// Formula for N is odd
ans = (N / 2 + 1) * 6
+ (N / 2) * 2;
}
// Print ans
System.out.print(ans +"\n");
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
findNthTerm(N);
}
}
// This code contributed by PrinciRaj1992
Python3
# Python3 program for the above approach
# Function to find Nth term
def findNthTerm(N):
ans = 0;
# Check if N is even
if (N % 2 == 0):
# Formula for n is even
ans = (N // 2) * 6 + (N // 2) * 2;
# Check if N is odd
else:
# Formula for N is odd
ans = (N // 2 + 1) * 6 + (N // 2) * 2;
# Print ans
print(ans);
# Driver Code
if __name__ == '__main__':
N = 3;
findNthTerm(N);
# This code is contributed by Rajput-Ji
C#
// C# program for the above approach
using System;
class GFG{
// Function to find Nth term
static void findNthTerm(int N)
{
int ans;
// Check if N is even
if (N % 2 == 0) {
// Formula for n is even
ans = (N / 2) * 6
+ (N / 2) * 2;
}
// Check if N is odd
else {
// Formula for N is odd
ans = (N / 2 + 1) * 6
+ (N / 2) * 2;
}
// Print ans
Console.Write(ans +"\n");
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
findNthTerm(N);
}
}
// This code is contributed by PrinciRaj1992
Java脚本
输出:
14
时间复杂度: O(1)