Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find out the prime numbers in the form of A+nB or B+nA

  • Last Updated : 21 May, 2021

Given two integers A and B and an integer N. The task is to find out N prime numbers of the form A + nB or B + nA( n=1, 2, 3…). If it is not possible, print -1.

Examples:  

Input: A = 3, B = 5, N = 4 
Output: 13, 11, 17, 23 
Explanation: 
13 (3+2*5) 
11 (5+2*3) 
17 (5+4*3) 
23 (3+4*5)

Input: A = 4, B = 6, N = 4 
Output: -1  

Approach: 
Since A + nB is a prime, one thing is sure that there should not present any common factor between A and B, which means A and B should be co-prime.
The best and most efficient approach will be to use Dirichlet’s Theorem.
Dirichlet’s Theorem says that if a and b are relatively prime positive integers, then the arithmetic sequence a, a+b, a+2b, a+3b…contains infinitely many primes.
So, firstly, check if A and B are co-prime. 
If A and B are co-prime, then check A+nB and B+nA for their primality, where n=1, 2, 3… Print the first N prime numbers among them. 

Below is the implementation of the above approach:  

C++




// C++ implementation of
// the above approach
#include <bits/stdc++.h>
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<int> 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<Integer> st = new HashSet<Integer>();
    // 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<int> st = new HashSet<int>();
     
    // 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

Javascript




<script>
// Javascript  implementation of
// the above approach
 
 
function __gcd(a, b) {
    if (b == 0)
        return a;
    return __gcd(b, a % b);
}
 
// Utility function to check
// whether two numbers is
// co-prime or not
function coprime(a, b) {
    if (__gcd(a, b) == 1)
        return true;
    else
        return false;
}
 
// Utility function to check
// whether a number is prime
// or not
function isPrime(n) {
    // Corner case
    if (n <= 1)
        return false;
 
    if (n == 2 || n == 3)
        return true;
 
    // Check from 2 to sqrt(n)
    for (let i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// finding the Prime numbers
function findNumbers(a, b, n) {
 
    let possible = true;
 
    // Checking whether given
    // numbers are co-prime
    // or not
    if (!coprime(a, b))
        possible = false;
 
    let c1 = 1;
    let c2 = 1;
 
    let num1, num2;
 
    // To store the N primes
    let st = new Set();
    // If 'possible' is true
    if (possible) {
 
        // Printing n numbers
        // of prime
        while (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++;
        }
 
        // Sort the Set by using spread operator and Array.sort() method
        st = [...st].sort((a, b) => a - b)
 
        for (let i of st)
            document.write(i + " ");
    }
 
    // If 'possible' is false
    // return -1
    else
        document.write("-1");
}
 
// Driver Code
let a = 3;
let b = 5;
let n = 4;
 
findNumbers(a, b, n);
 
// This code is contributed by _saurabh_jaiswal
 
 
</script>
Output: 
11 13 17 23

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!