📌  相关文章
📜  查找系列的第N个项,其中每个项交替相差6和2

📅  最后修改于: 2021-04-23 06:02:52             🧑  作者: Mango

给定数字N ,任务是找到级数的第N个项,其中每个项交替相差6和2。
例子:

天真的方法:这个想法是从1开始62的增量迭代,直到达到第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个项:

  1. 如果N为奇数:第N个项由(N / 2 +1)* 6 +(N / 2)* 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)