Find a prime number S containing given number N in it
Last Updated :
29 Sep, 2022
Given an integer N, find a prime number S such that all digits of N occur in a contiguous sequence. There may be multiple answers. Print any one of them.
Example:
Input: N = 42
Output: 42013
Explanation: 42013 is a prime and 42 occurs as a contiguous number in it. 15427 is also a correct answer.
Input: N = 47
Output: 47
Explanation: 47 itself is a prime
Naive Approach: Below steps can be followed:
- Iterate through all the numbers starting from N
- Convert every number into a string with to_string() function
- Check for the required substring using str.find() function
- If there is any number that has N as a substring and it is prime then return that number
Time Complexity: O(S), where S is the required prime number
Efficient Approach: Below steps can be followed:
- The fact can be used that a number with value upto 1e12, between two consecutive primes, there are at most 464 non-prime numbers.
- Extend the current number N by multiplying by 1000.
- After that iterate through the next numbers one by one and check each of them.
- If the number is prime then print that number.
- It is easy to see that the first condition will always follow as the digits except the last three will be N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( long long N)
{
if (N == 1)
return false ;
for ( long long i = 2; i <= sqrt (N); i++)
if (N % i == 0)
return false ;
return true ;
}
long long prime_substring_Number( long long N)
{
if (N == 0) {
return 103;
}
N *= 1000;
for ( long long i = N; i < N + 465; i++) {
if (isPrime(i)) {
return i;
}
}
return 0;
}
int main()
{
long N = 42;
cout << prime_substring_Number(N);
}
|
Java
import java.io.*;
class GFG {
static boolean isPrime( long N)
{
if (N == 1 )
return false ;
for ( long i = 2 ; i <= Math.sqrt(N); i++)
if (N % i == 0 )
return false ;
return true ;
}
static long prime_substring_Number( long N)
{
if (N == 0 ) {
return 103 ;
}
N *= 1000 ;
for ( long i = N; i < N + 465 ; i++) {
if (isPrime(i)) {
return i;
}
}
return 0 ;
}
public static void main(String[] args)
{
long N = 42 ;
System.out.println(prime_substring_Number(N));
}
}
|
Python3
from math import *
def isPrime(N) :
if N > 1 :
for i in range ( 2 , int (N / 2 ) + 1 ):
if (N % i) = = 0 :
return False
else :
return True
else :
return False
def prime_substring_Number(N) :
if (N = = 0 ) :
return 103
N = N * 1000
for i in range (N,N + 465 ):
if (isPrime(i)) :
return i
return 0
N = 42
print (prime_substring_Number(N))
|
C#
using System;
class GFG {
static bool isPrime( long N)
{
if (N == 1)
return false ;
for ( long i = 2; i <= Math.Sqrt(N); i++)
if (N % i == 0)
return false ;
return true ;
}
static long prime_substring_Number( long N)
{
if (N == 0) {
return 103;
}
N *= 1000;
for ( long i = N; i < N + 465; i++) {
if (isPrime(i)) {
return i;
}
}
return 0;
}
public static void Main()
{
long N = 42;
Console.WriteLine(prime_substring_Number(N));
}
}
|
Javascript
<script>
function isPrime(N) {
if (N == 1)
return false ;
for (let i = 2; i <= Math.sqrt(N); i++)
if (N % i == 0)
return false ;
return true ;
}
function prime_substring_Number(N)
{
if (N == 0) {
return 103;
}
N *= 1000;
for (let i = N; i < N + 465; i++) {
if (isPrime(i)) {
return i;
}
}
return 0;
}
let N = 42;
document.write(prime_substring_Number(N));
</script>
|
Time Complexity: O(sqrt(N*1000)*300)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...