给定整数N ,任务是检查是否有可能通过从N中删除任何一位来使N为素数。
例子:
Input: N = 610
Output: Yes
Explanation:
Deleting 0 from 610, we get 61 which is prime.
Input: N = 68
Output: No
方法:这个想法是将N转换为字符串。现在迭代在指数字符串,并删除字符的每个数字从我的字符串,然后转换成字符串索引中删除字符,我到一个整数后,现在检查如果这个整数是一个素数,则返回true。否则,最后返回false。
下面是上述方法的实现:
C++
// C++ implementation to check if a number
// becomes prime by deleting any digit
#include
using namespace std;
// Function to check if N is prime
bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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;
}
// Function to delete character at index i
// from given string str
string deleteIth(string str, int i)
{
// Deletes character at position 4
str.erase(str.begin() + i);
return str;
}
// Function to check if a number
// becomes prime by deleting any digit
bool isPrimePossible(int N)
{
// Converting the number to string
string s = to_string(N);
// length of string
int l = s.length();
// number should not be
// of single digit
if (l < 2)
return false;
// Loop to find all numbers
// after deleting a single digit
for (int i = 0; i < l ; i++) {
// Deleting ith character
// from the string
string str = deleteIth(s, i);
// converting string to int
int num = stoi(str);
if (isPrime(num))
return true;
}
return false;
}
// Driver Code
int main()
{
int N = 610;
isPrimePossible(N) ? cout << "Yes"
: cout << "No";
return 0;
}
Java
// Java implementation to check if a number
// becomes prime by deleting any digit
import java.util.*;
class GFG{
// Function to check if N is prime
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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;
}
// Function to delete character at index i
// from given String str
static String deleteIth(String str, int i)
{
// Deletes character at position 4
str = str.substring(0, i) +
str.substring(i + 1);
return str;
}
// Function to check if a number
// becomes prime by deleting any digit
static boolean isPrimePossible(int N)
{
// Converting the number to String
String s = String.valueOf(N);
// length of String
int l = s.length();
// number should not be
// of single digit
if (l < 2)
return false;
// Loop to find all numbers
// after deleting a single digit
for (int i = 0; i < l; i++)
{
// Deleting ith character
// from the String
String str = deleteIth(s, i);
// converting String to int
int num = Integer.valueOf(str);
if (isPrime(num))
return true;
}
return false;
}
// Driver Code
public static void main(String[] args)
{
int N = 610;
if (isPrimePossible(N))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation to check if a number
# becomes prime by deleting any digit
# Function to check if N is prime
#from builtins import range
def isPrime(n):
# Corner cases
if (n <= 1):
return False;
if (n <= 3):
return True;
# This is checked so that we can skip
# middle five numbers in below loop
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;
# Function to delete character at index i
# from given String str
def deleteIth(str, i):
# Deletes character at position 4
str = str[0:i] + str[i + 1:];
return str;
# Function to check if a number
# becomes prime by deleting any digit
def isPrimePossible(N):
# Converting the number to String
s = str(N);
# length of String
l = len(s);
# number should not be
# of single digit
if (l < 2):
return False;
# Loop to find all numbers
# after deleting a single digit
for i in range(l):
# Deleting ith character
# from the String
str1 = deleteIth(s, i);
# converting String to int
num = int(str1);
if (isPrime(num)):
return True;
return False;
# Driver Code
if __name__ == '__main__':
N = 610;
if (isPrimePossible(N)):
print("Yes");
else:
print("No");
# This code is contributed by Rajput-Ji
C#
// C# implementation to check if a number
// becomes prime by deleting any digit
using System;
class GFG{
// Function to check if N is prime
static bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
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;
}
// Function to delete character at index i
// from given String str
static String deleteIth(String str, int i)
{
// Deletes character at position 4
str = str.Substring(0, i) +
str.Substring(i + 1);
return str;
}
// Function to check if a number
// becomes prime by deleting any digit
static bool isPrimePossible(int N)
{
// Converting the number to String
String s = String.Join("", N);
// length of String
int l = s.Length;
// number should not be
// of single digit
if (l < 2)
return false;
// Loop to find all numbers
// after deleting a single digit
for(int i = 0; i < l; i++)
{
// Deleting ith character
// from the String
String str = deleteIth(s, i);
// converting String to int
int num = Int32.Parse(str);
if (isPrime(num))
return true;
}
return false;
}
// Driver Code
public static void Main(String[] args)
{
int N = 610;
if (isPrimePossible(N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Rajput-Ji
输出
Yes
时间复杂度: O(D)
辅助空间: O(1)