Check if number can be made prime by deleting a single digit
Last Updated :
07 May, 2021
Given an integer N, the task is to check if it is possible to make N prime by deleting any single digit from N.
Examples:
Input: N = 610
Output: Yes
Explanation:
Deleting 0 from 610, we get 61 which is prime.
Input: N = 68
Output: No
Approach: The idea is to convert N to a string. Now iterate for every digit of string and Delete character at index i from string and then convert the string after deleting character at index i to an integer, Now check if this integer is a prime, then return true. Otherwise, finally return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
string deleteIth(string str, int i)
{
str.erase(str.begin() + i);
return str;
}
bool isPrimePossible( int N)
{
string s = to_string(N);
int l = s.length();
if (l < 2)
return false ;
for ( int i = 0; i < l ; i++) {
string str = deleteIth(s, i);
int num = stoi(str);
if (isPrime(num))
return true ;
}
return false ;
}
int main()
{
int N = 610;
isPrimePossible(N) ? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static String deleteIth(String str, int i)
{
str = str.substring( 0 , i) +
str.substring(i + 1 );
return str;
}
static boolean isPrimePossible( int N)
{
String s = String.valueOf(N);
int l = s.length();
if (l < 2 )
return false ;
for ( int i = 0 ; i < l; i++)
{
String str = deleteIth(s, i);
int num = Integer.valueOf(str);
if (isPrime(num))
return true ;
}
return false ;
}
public static void main(String[] args)
{
int N = 610 ;
if (isPrimePossible(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isPrime(n):
if (n < = 1 ):
return False ;
if (n < = 3 ):
return True ;
if (n % 2 = = 0 or n % 3 = = 0 ):
return False ;
for i in range ( 5 , int (n * * 1 / 2 ), 6 ):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False ;
return True ;
def deleteIth( str , i):
str = str [ 0 :i] + str [i + 1 :];
return str ;
def isPrimePossible(N):
s = str (N);
l = len (s);
if (l < 2 ):
return False ;
for i in range (l):
str1 = deleteIth(s, i);
num = int (str1);
if (isPrime(num)):
return True ;
return False ;
if __name__ = = '__main__' :
N = 610 ;
if (isPrimePossible(N)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static String deleteIth(String str, int i)
{
str = str.Substring(0, i) +
str.Substring(i + 1);
return str;
}
static bool isPrimePossible( int N)
{
String s = String.Join( "" , N);
int l = s.Length;
if (l < 2)
return false ;
for ( int i = 0; i < l; i++)
{
String str = deleteIth(s, i);
int num = Int32.Parse(str);
if (isPrime(num))
return true ;
}
return false ;
}
public static void Main(String[] args)
{
int N = 610;
if (isPrimePossible(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function deleteIth(str, i)
{
str = str.substr(0, i) +
str.substr(i + 1);
return str;
}
function isPrimePossible(N)
{
let s = N.toString();
let l = s.length;
if (l < 2)
return false ;
for (let i = 0; i < l; i++)
{
let str = deleteIth(s, i);
let num = parseInt(str);
if (isPrime(num))
return true ;
}
return false ;
}
let N = 610;
if (isPrimePossible(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(D)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...