📜  通过删除给定数字的数字形成的唯一质数的计数

📅  最后修改于: 2021-10-28 01:45:16             🧑  作者: Mango

给定一个数字N,任务是计算通过删除给定数字的零个或多个数字可以形成的唯一质数的数量。

例子:

方法:这个问题可以使用递归来解决,因为对于每个数字都有两种选择,包括数字或不包括数字。对于每个选择,检查形成的数字是否是质数。请按照以下步骤解决此问题:

  • 初始化一个 HashSet 说Primes来存储唯一的素数。
  • 声明一个函数,比如 uniquePrimeNums(number, ans, index) ,传递数字字符串N,ans作为空字符串和索引 0作为参数。
    • 基本情况:如果该指数达到字符串的长度,然后将字符串转换为整数,并检查形成的数是否是素与否,如果素数,则添加在HashSet的素数的数量。
    • 通过获取字符即uniquePrimeNums(number, ans + number.charAt(index), index + 1)或保留字符即uniquePrimeNums(number, ans, index + 1) ,为这两种选择调用uniquePrimeNums函数。
  • 完成上述步骤后,打印HashSet Primes大小作为所需答案。

下面是上述方法的实现:

Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // Function to check whether the number
    // is prime or not
    static boolean checkprime(int n)
    {
        // If n is 1
        if (n == 1) {
            return false;
        }
 
        // If n is 2 or 3
        if (n == 2 || n == 3) {
            return true;
        }
 
        // If n is multiple of 2, 3 or 6
        else if (n % 2 == 0 || n % 3 == 0
                 || n % 6 == 0) {
            return false;
        }
 
        // Traversing till sqrt(n)
        for (int i = 6; i * i <= n; i += 6) {
            if (n % (i - 1) == 0
                || n % (i + 1) == 0) {
                return false;
            }
        }
        return true;
    }
 
    // To store the unique prime numbers
    static HashSet Primes
        = new HashSet<>();
 
    // Function to Count the total number
    // of unique prime number formed by
    // deleting zero or more digits of the
    // given number
    static void uniquePrimeNums(
        String number, String ans, int index)
    {
        // Base Case
        if (index == number.length()) {
 
            if (ans.length() != 0)
 
                // Check whether the number is
                // prime or not
                if (checkprime(Integer.parseInt(ans))) {
 
                    // Adding to the HashSet
                    Primes.add(Integer.parseInt(ans));
                }
 
            return;
        }
 
        // Recursive call by taking the character
        // at index
        uniquePrimeNums(number,
                        ans + number.charAt(index),
                        index + 1);
 
        // Recursive call by not taking the
        // character
        uniquePrimeNums(number, ans, index + 1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Input
        int number = 132;
 
        // Function Call
        uniquePrimeNums("" + number, "", 0);
        System.out.println(Primes.size());
    }
}


Python3
# Python3 program for the above approach
import math
 
# Function to check whether the number
# is prime or not
def checkprime(n):
 
    # If n is 1
    if (n == 1):
        return False
 
    # If n is 2 or 3
    if (n == 2 or n == 3):
        return True
 
    # If n is multiple of 2, 3 or 6
    elif (n % 2 == 0 or n % 3 == 0 or
          n % 6 == 0):
        return False
 
    # Traversing till sqrt(n)
    k = int(math.sqrt(n))
    for i in range(6, k+1, 6):
        if (n % (i - 1) == 0 or n % (i + 1) == 0):
            return False
             
    return True
   
# Function to Count the total number
# of unique prime number formed by
# deleting zero or more digits of the
# given number
def uniquePrimeNums(number, ans, index):
   
    # Base Case
    length = len(list(number))
     
    if (index == length):
        if (len(ans) != 0):
             
            # Check whether the number is
            # prime or not
            if (checkprime(int(ans))):
                 
                # Adding to the HashSet
                Primes.add(int(ans))
        return
       
    # Recursive call by taking the character
    # at index
    uniquePrimeNums(number, ans + number[index],
                          index + 1)
     
    # Recursive call by not taking the
    # character
    uniquePrimeNums(number, ans, index + 1)
    return
 
# To store the unique prime numbers
Primes = set()
 
# Driver code
if __name__ == '__main__':
   
    # Given Input
    number = 132
     
    # Function Call
    uniquePrimeNums(str(number), "", 0)
    print(len(Primes))
 
# This code is contributed by MuskanKalra1


C#
using System;
using System.Collections.Generic;
public class GFG {
    static bool checkprime(int n)
    {
        // If n is 1
        if (n == 1) {
            return false;
        }
 
        // If n is 2 or 3
        if (n == 2 || n == 3) {
            return true;
        }
 
        // If n is multiple of 2, 3 or 6
        else if (n % 2 == 0 || n % 3 == 0 || n % 6 == 0) {
            return false;
        }
 
        // Traversing till sqrt(n)
        for (int i = 6; i * i <= n; i += 6) {
            if (n % (i - 1) == 0 || n % (i + 1) == 0) {
                return false;
            }
        }
        return true;
    }
 
    // To store the unique prime numbers
    static HashSet Primes = new HashSet();
 
    // Function to Count the total number
    // of unique prime number formed by
    // deleting zero or more digits of the
    // given number
    static void uniquePrimeNums(String number, String ans,
                                int index)
    {
        // Base Case
        if (index == number.Length) {
 
            if (ans.Length != 0)
 
                // Check whether the number is
                // prime or not
                if (checkprime(int.Parse(ans))) {
 
                    // Adding to the HashSet
                    Primes.Add(int.Parse(ans));
                }
 
            return;
        }
 
        // Recursive call by taking the character
        // at index
        uniquePrimeNums(number, ans + number[index],
                        index + 1);
 
        // Recursive call by not taking the
        // character
        uniquePrimeNums(number, ans, index + 1);
    }
 
    // Driver Code
    static public void Main()
    {
        int number = 132;
 
        // Function Call
        uniquePrimeNums("" + number, "", 0);
        Console.WriteLine(Primes.Count);
    }
}
 
// This code is contributed by maddler.


Javascript


输出
3

时间复杂度: O(2 N * sqrt(N))
辅助空间: O(2 N )