Compruebe si el número se puede convertir en primo eliminando un solo dígito

Dado un número entero N , la tarea es verificar si es posible hacer que N sea primo eliminando cualquier dígito único de N .

Ejemplos:

Entrada: N = 610
Salida:
Explicación: 
Borrando 0 de 610, obtenemos 61 que es primo. 

Entrada: N = 68 
Salida: No
 

 

Enfoque: La idea es convertir N en una string. Ahora itere para cada dígito de la string y elimine el carácter en el índice i de la string y luego convierta la string después de eliminar el carácter en el índice i a un número entero. Ahora verifique si este número entero es un primo, luego devuelva verdadero. De lo contrario, finalmente devuelve falso.
 

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ implementation to check if a number
// becomes prime by deleting any digit
 
#include <bits/stdc++.h>
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

Javascript

<script>
// JavaScript implementation to check if a number
// becomes prime by deleting any digit
 
// Function to check if N is prime
  function 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 || 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 to delete character at index i
  // from given String str
  function deleteIth(str, i)
  {
    // Deletes character at position 4
    str = str.substr(0, i) +
            str.substr(i + 1);
  
    return str;
  }
  
  // Function to check if a number
  // becomes prime by deleting any digit
 function isPrimePossible(N)
  {
    // Converting the number to String
    let s = N.toString();
  
    // length of String
    let 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 (let i = 0; i < l; i++)
    {
  
      // Deleting ith character
      // from the String
      let str = deleteIth(s, i);
  
      // converting String to let
      let num = parseInt(str);
  
      if (isPrime(num))
        return true;
    }
    return false;
  }
 
// Driver Code
 
    let N = 610;
    if (isPrimePossible(N))
      document.write("Yes");
    else
      document.write("No");
      
</script>
Producción

Yes

Complejidad temporal: O(D)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por spp____ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *