Find out the prime numbers in the form of A+nB or B+nA
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++
#include <bits/stdc++.h>
using namespace std;
int coprime( int a, int b)
{
if (__gcd(a, b) == 1)
return true ;
else
return false ;
}
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n == 2 or n == 3)
return true ;
for ( int i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
void findNumbers( int a, int b, int n)
{
bool possible = true ;
if (!coprime(a, b))
possible = false ;
int c1 = 1;
int c2 = 1;
int num1, num2;
set< int > st;
if (possible) {
while (( int )st.size() != n) {
num1 = a + (c1 * b);
if (isPrime(num1)) {
st.insert(num1);
}
c1++;
num2 = b + (c2 * a);
if (isPrime(num2)) {
st.insert(num2);
}
c2++;
}
for ( int i : st)
cout << i << " " ;
}
else
cout << "-1" ;
}
int main()
{
int a = 3;
int b = 5;
int n = 4;
findNumbers(a, b, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
static boolean coprime( int a, int b)
{
if (__gcd(a, b) == 1 )
return true ;
else
return false ;
}
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n == 2 || n == 3 )
return true ;
for ( int i = 2 ; i * i <= n; i++)
if (n % i == 0 )
return false ;
return true ;
}
static void findNumbers( int a, int b, int n)
{
boolean possible = true ;
if (!coprime(a, b))
possible = false ;
int c1 = 1 ;
int c2 = 1 ;
int num1, num2;
HashSet<Integer> st = new HashSet<Integer>();
if (possible)
{
while (( int )st.size() != n)
{
num1 = a + (c1 * b);
if (isPrime(num1))
{
st.add(num1);
}
c1++;
num2 = b + (c2 * a);
if (isPrime(num2))
{
st.add(num2);
}
c2++;
}
for ( int i : st)
System.out.print(i + " " );
}
else
System.out.print( "-1" );
}
public static void main(String[] args)
{
int a = 3 ;
int b = 5 ;
int n = 4 ;
findNumbers(a, b, n);
}
}
|
Python3
from math import gcd, sqrt
def coprime(a, b) :
if (gcd(a, b) = = 1 ) :
return True ;
else :
return False ;
def isPrime(n) :
if (n < = 1 ) :
return False ;
if (n = = 2 or n = = 3 ) :
return True ;
for i in range ( 2 , int (sqrt(n)) + 1 ) :
if (n % i = = 0 ) :
return False ;
return True ;
def findNumbers(a, b, n) :
possible = True ;
if ( not coprime(a, b)) :
possible = False ;
c1 = 1 ;
c2 = 1 ;
num1 = 0 ;
num2 = 0 ;
st = set ();
if (possible) :
while ( len (st) ! = n) :
num1 = a + (c1 * b);
if (isPrime(num1)):
st.add(num1);
c1 + = 1 ;
num2 = b + (c2 * a);
if (isPrime(num2)):
st.add(num2);
c2 + = 1 ;
for i in st :
print (i, end = " " );
else :
print ( "-1" );
if __name__ = = "__main__" :
a = 3 ;
b = 5 ;
n = 4 ;
findNumbers(a, b, n);
|
C#
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);
}
static bool coprime( int a, int b)
{
if (__gcd(a, b) == 1)
return true ;
else
return false ;
}
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n == 2 || n == 3)
return true ;
for ( int i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
static void findNumbers( int a, int b, int n)
{
bool possible = true ;
if (!coprime(a, b))
possible = false ;
int c1 = 1;
int c2 = 1;
int num1, num2;
HashSet< int > st = new HashSet< int >();
if (possible)
{
while (st.Count != n)
{
num1 = a + (c1 * b);
if (isPrime(num1))
{
st.Add(num1);
}
c1++;
num2 = b + (c2 * a);
if (isPrime(num2))
{
st.Add(num2);
}
c2++;
}
foreach ( int i in st)
Console.Write(i + " " );
}
else
Console.Write( "-1" );
}
public static void Main(String[] args)
{
int a = 3;
int b = 5;
int n = 4;
findNumbers(a, b, n);
}
}
|
Javascript
<script>
function __gcd(a, b) {
if (b == 0)
return a;
return __gcd(b, a % b);
}
function coprime(a, b) {
if (__gcd(a, b) == 1)
return true ;
else
return false ;
}
function isPrime(n) {
if (n <= 1)
return false ;
if (n == 2 || n == 3)
return true ;
for (let i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
function findNumbers(a, b, n) {
let possible = true ;
if (!coprime(a, b))
possible = false ;
let c1 = 1;
let c2 = 1;
let num1, num2;
let st = new Set();
if (possible) {
while (st.size != n) {
num1 = a + (c1 * b);
if (isPrime(num1)) {
st.add(num1);
}
c1++;
num2 = b + (c2 * a);
if (isPrime(num2)) {
st.add(num2);
}
c2++;
}
st = [...st].sort((a, b) => a - b)
for (let i of st)
document.write(i + " " );
}
else
document.write( "-1" );
}
let a = 3;
let b = 5;
let n = 4;
findNumbers(a, b, n);
</script>
|
Time Complexity: O(n*sqrt(n))
Auxiliary Space: O(n)
Last Updated :
19 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...