📌  相关文章
📜  程序以找到系列0、2、1、3、1、5、2、7、3等中的第N个项

📅  最后修改于: 2021-04-27 09:22:44             🧑  作者: Mango

给定数字N。任务是编写一个程序来查找以下系列中的第N个术语:

例子:

Input: N = 5
Output: 1

Input: N = 10
Output: 11

当我们仔细查看系列时,我们发现该系列是2个系列的混合体:

  1. 给定系列中奇数位置的术语形成斐波那契数列。
  2. 给定系列中偶数位置的术语形成一系列质数。

现在,要解决上述问题,请首先检查输入数字N是偶数还是奇数。

  • 如果是奇数,则设置N =(N / 2)+ 1(因为有两个系列并行运行),然后找到第N斐波那契数。
  • 如果N是偶数,只需设置N = N / 2并找到第N个素数。

下面是上述方法的实现:

C++
// CPP program to find N-th term
// in the series
#include
#define MAX 1000
using namespace std;
 
// Function to find Nth Prime Number
int NthPrime(int n)
{
    int count = 0;
    for (int i = 2; i <= MAX; i++) {
        int check = 0;
        for (int j = 2; j <= sqrt(i); j++) {
            if (i % j == 0) {
                check = 1;
                break;
            }
        }
        if (check == 0)
            count++;
 
        if (count == n) {
            return i;
            break;
        }
    }
}
 
// Function to find Nth Fibonacci Number
int NthFib(int n)
{
    // Declare an array to store
    // Fibonacci numbers.
    int f[n + 2];
    int i;
 
    // 0th and 1st number of the
    // series are 0 and 1
    f[0] = 0;
    f[1] = 1;
 
    for (i = 2; i <= n; i++) {
        f[i] = f[i - 1] + f[i - 2];
    }
 
    return f[n];
}
 
// Function to find N-th term
// in the series
void findNthTerm(int n)
{
    // If n is even
    if (n % 2 == 0) {
        n = n / 2;
        n = NthPrime(n);
        cout << n << endl;
    }
 
    // If n is odd
    else {
        n = (n / 2) + 1;
        n = NthFib(n - 1);
        cout << n << endl;
    }
}
 
// Driver code
int main()
{
    int X = 5;
    findNthTerm(X);
 
    X = 10;
    findNthTerm(X);
 
    return 0;
}


Java
// Java program to find N-th
// term in the series
class GFG
{
 
static int MAX = 1000;
 
// Function to find Nth Prime Number
static int NthPrime(int n)
{
int count = 0;
int i;
for (i = 2; i <= MAX; i++)
{
    int check = 0;
    for (int j = 2; j <= Math.sqrt(i); j++)
    {
        if (i % j == 0)
        {
            check = 1;
            break;
        }
    }
    if (check == 0)
        count++;
 
    if (count == n)
    {
        return i;
         
    }
}
    return 0;
}
 
// Function to find Nth Fibonacci Number
static int NthFib(int n)
{
// Declare an array to store
// Fibonacci numbers.
int []f = new int[n + 2];
int i;
 
// 0th and 1st number of the
// series are 0 and 1
f[0] = 0;
f[1] = 1;
 
for (i = 2; i <= n; i++)
{
    f[i] = f[i - 1] + f[i - 2];
}
 
return f[n];
}
 
// Function to find N-th term
// in the series
static void findNthTerm(int n)
{
// If n is even
if (n % 2 == 0)
{
    n = n / 2;
    n = NthPrime(n);
    System.out.println(n);
}
 
// If n is odd
else
{
    n = (n / 2) + 1;
    n = NthFib(n - 1);
    System.out.println(n);
}
}
 
// Driver code
public static void main(String[] args)
{
    int X = 5;
    findNthTerm(X);
 
    X = 10;
    findNthTerm(X);
}
}
 
// This code is contributed
// by ChitraNayal


Python 3
# Python 3 program to find N-th
# term in the series
 
# import sqrt method from math module
from math import sqrt
 
# Globally declare constant value
MAX = 1000
 
# Function to find Nth Prime Number
def NthPrime(n) :
     
    count = 0
    for i in range(2, MAX + 1) :
         
        check = 0
        for j in range(2, int(sqrt(i)) + 1) :
             
            if i % j == 0 :
                check = 1
                break
 
        if check == 0 :
            count += 1
 
        if count == n :
            return i
            break
 
# Function to find Nth Fibonacci Number
def NthFib(n) :
 
    # Create a list of size n+2
    # to store Fibonacci numbers.
    f = [0] * (n + 2)
 
    # 0th and 1st number of the
    # series are 0 and 1
    f[0], f[1] = 0, 1
 
    for i in range(2, n + 1) :
 
        f[i] = f[i - 1] + f[i - 2]
 
    return f[n]
 
# Function to find N-th
# term in the series
def findNthTerm(n) :
 
    # If n is even
    if n % 2 == 0 :
        n //= 2
        n = NthPrime(n)
        print(n)
 
    # If n is odd
    else :
        n = (n // 2) + 1
        n = NthFib(n - 1)
        print(n)
 
# Driver code
if __name__ == "__main__" :
 
    X = 5
 
    # function calling
    findNthTerm(X)
 
    X = 10
    findNthTerm(X)
     
# This code is contributed by ANKITRAI1


C#
// C# program to find N-th term
// in the series
using System;
 
class GFG
{
static int MAX = 1000;
 
// Function to find Nth Prime Number
static int NthPrime(int n)
{
int count = 0;
int i;
for ( i = 2; i <= MAX; i++)
{
    int check = 0;
    for (int j = 2; j <= Math.Sqrt(i); j++)
    {
        if (i % j == 0)
        {
            check = 1;
            break;
        }
    }
    if (check == 0)
        count++;
 
    if (count == n)
    {
        return i;
    }
}
    return 0;
}
 
// Function to find Nth Fibonacci Number
static int NthFib(int n)
{
     
// Declare an array to store
// Fibonacci numbers.
int []f = new int[n + 2];
int i;
 
// 0th and 1st number of the
// series are 0 and 1
f[0] = 0;
f[1] = 1;
 
for (i = 2; i <= n; i++)
{
    f[i] = f[i - 1] + f[i - 2];
}
 
return f[n];
}
 
// Function to find N-th term
// in the series
static void findNthTerm(int n)
{
// If n is even
if (n % 2 == 0)
{
    n = n / 2;
    n = NthPrime(n);
    Console.WriteLine(n);
}
 
// If n is odd
else
{
    n = (n / 2) + 1;
    n = NthFib(n - 1);
    Console.WriteLine(n);
}
}
 
// Driver code
public static void Main()
{
    int X = 5;
    findNthTerm(X);
 
    X = 10;
    findNthTerm(X);
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


输出:
1
11