📜  找出A + nB或B + nA形式的质数

📅  最后修改于: 2021-04-21 21:08:35             🧑  作者: Mango

给定两个整数AB以及一个整数N。任务是找出形式为A + nBB + nA的N个素数( n = 1、2、3…)。如果不可能,请打印-1。

例子:

方法:
由于A + nB是质数,因此确保AB之间不应存在任何公因子,这意味着AB应该是互质的。

最好,最有效的方法是使用Dirichlet定理

Dirichlet定理说,如果ab是相对质数的正整数,则算术序列aa + ba + 2ba + 3b …包含无限多个质数。

因此,首先检查AB是否互质。
如果AB是互质的,则检查A + nBB + nA的素数,其中n = 1、2、3…。打印其中的前N个质数。
下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
#include 
using namespace std;
  
// Utility function to check
// whether two numbers is
// co-prime or not
int coprime(int a, int b)
{
    if (__gcd(a, b) == 1)
        return true;
    else
        return false;
}
  
// Utility function to check
// whether a number is prime
// or not
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
  
    if (n == 2 or n == 3)
        return true;
  
    // Check from 2 to sqrt(n)
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// finding the Prime numbers
void findNumbers(int a, int b, int n)
{
  
    bool possible = true;
  
    // Checking whether given
    // numbers are co-prime
    // or not
    if (!coprime(a, b))
        possible = false;
  
    int c1 = 1;
    int c2 = 1;
  
    int num1, num2;
  
    // To store the N primes
    set st;
    // If 'possible' is true
    if (possible) {
  
        // Printing n numbers
        // of prime
        while ((int)st.size() != n) {
  
            // checking the form of a+nb
            num1 = a + (c1 * b);
            if (isPrime(num1)) {
                st.insert(num1);
            }
            c1++;
  
            // Checking the form of b+na
            num2 = b + (c2 * a);
            if (isPrime(num2)) {
                st.insert(num2);
            }
            c2++;
        }
  
        for (int i : st)
            cout << i << " ";
    }
  
    // If 'possible' is false
    // return -1
    else
        cout << "-1";
}
  
// Driver Code
int main()
{
  
    int a = 3;
    int b = 5;
    int n = 4;
  
    findNumbers(a, b, n);
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int __gcd(int a, int b) 
{ 
    if (b == 0) 
        return a; 
    return __gcd(b, a % b); 
}
  
// Utility function to check
// whether two numbers is
// co-prime or not
static boolean coprime(int a, int b)
{
    if (__gcd(a, b) == 1)
        return true;
    else
        return false;
}
  
// Utility function to check
// whether a number is prime
// or not
static boolean isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
  
    if (n == 2 || n == 3)
        return true;
  
    // Check from 2 to sqrt(n)
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// finding the Prime numbers
static void findNumbers(int a, int b, int n)
{
    boolean possible = true;
  
    // Checking whether given
    // numbers are co-prime
    // or not
    if (!coprime(a, b))
        possible = false;
  
    int c1 = 1;
    int c2 = 1;
  
    int num1, num2;
  
    // To store the N primes
    HashSet st = new HashSet();
    // If 'possible' is true
    if (possible)
    {
  
        // Printing n numbers
        // of prime
        while ((int)st.size() != n)
        {
  
            // checking the form of a+nb
            num1 = a + (c1 * b);
            if (isPrime(num1))
            {
                st.add(num1);
            }
            c1++;
  
            // Checking the form of b+na
            num2 = b + (c2 * a);
            if (isPrime(num2)) 
            {
                st.add(num2);
            }
            c2++;
        }
  
        for (int i : st)
            System.out.print(i + " ");
    }
  
    // If 'possible' is false
    // return -1
    else
        System.out.print("-1");
}
  
// Driver Code
public static void main(String[] args) 
{
    int a = 3;
    int b = 5;
    int n = 4;
  
    findNumbers(a, b, n);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the above approach 
from math import gcd, sqrt
  
# Utility function to check 
# whether two numbers is 
# co-prime or not 
def coprime(a, b) :
      
    if (gcd(a, b) == 1) :
        return True; 
          
    else :
        return False; 
  
# Utility function to check 
# whether a number is prime 
# or not 
def isPrime(n) : 
  
    # Corner case 
    if (n <= 1) :
        return False; 
  
    if (n == 2 or n == 3) :
        return True; 
  
    # Check from 2 to sqrt(n) 
    for i in range(2, int(sqrt(n)) + 1) :
        if (n % i == 0) :
            return False; 
  
    return True; 
  
# finding the Prime numbers 
def findNumbers(a, b, n) :
  
    possible = True; 
  
    # Checking whether given 
    # numbers are co-prime 
    # or not 
    if (not coprime(a, b)) :
        possible = False; 
  
    c1 = 1; 
    c2 = 1; 
  
    num1 = 0;
    num2 = 0; 
  
    # To store the N primes 
    st = set(); 
      
    # If 'possible' is true 
    if (possible) :
  
        # Printing n numbers 
        # of prime 
        while (len(st) != n) :
  
            # checking the form of a+nb 
            num1 = a + (c1 * b); 
              
            if (isPrime(num1)):
                  
                st.add(num1); 
                  
            c1 += 1; 
  
            # Checking the form of b+na 
            num2 = b + (c2 * a); 
              
            if (isPrime(num2)):
                st.add(num2); 
      
            c2 += 1; 
  
        for i in st :
            print(i, end = " "); 
  
    # If 'possible' is false 
    # return -1 
    else :
        print("-1"); 
  
# Driver Code 
if __name__ == "__main__" : 
  
    a = 3; 
    b = 5; 
    n = 4; 
  
    findNumbers(a, b, n); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
static int __gcd(int a, int b) 
{ 
    if (b == 0) 
        return a; 
    return __gcd(b, a % b); 
}
  
// Utility function to check
// whether two numbers is
// co-prime or not
static bool coprime(int a, int b)
{
    if (__gcd(a, b) == 1)
        return true;
    else
        return false;
}
  
// Utility function to check
// whether a number is prime
// or not
static bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
  
    if (n == 2 || n == 3)
        return true;
  
    // Check from 2 to sqrt(n)
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// finding the Prime numbers
static void findNumbers(int a, int b, int n)
{
    bool possible = true;
  
    // Checking whether given
    // numbers are co-prime
    // or not
    if (!coprime(a, b))
        possible = false;
  
    int c1 = 1;
    int c2 = 1;
  
    int num1, num2;
  
    // To store the N primes
    HashSet st = new HashSet();
      
    // If 'possible' is true
    if (possible)
    {
  
        // Printing n numbers
        // of prime
        while (st.Count != n)
        {
  
            // checking the form of a+nb
            num1 = a + (c1 * b);
            if (isPrime(num1))
            {
                st.Add(num1);
            }
            c1++;
  
            // Checking the form of b+na
            num2 = b + (c2 * a);
            if (isPrime(num2)) 
            {
                st.Add(num2);
            }
            c2++;
        }
  
        foreach (int i in st)
            Console.Write(i + " ");
    }
  
    // If 'possible' is false
    // return -1
    else
        Console.Write("-1");
}
  
// Driver Code
public static void Main(String[] args) 
{
    int a = 3;
    int b = 5;
    int n = 4;
  
    findNumbers(a, b, n);
}
}
  
// This code is contributed by 29AjayKumar


输出:
11 13 17 23