计算可以使用给定数字的数字形成的所有素数
给定一个由N个数字组成的字符串S ,任务是找到可以使用字符串S的数字形成的不同质数的数量。
例子:
Input: S = “123”
Output: 5
Explanation:
The prime numbers that can be formed from the digits of the string S is 2, 3, 13, 23, and 31. Hence, the total count is 5.
Input: S = “1”
Output: 0
方法:给定的问题可以通过使用深度优先搜索和回溯来找到所有可能的排列并检查它们是否可以形成素数来解决。请按照以下步骤解决问题:
- 初始化 HashSet H以存储可能的唯一素数字符串。
- 定义一个函数check(字符串 number)来检查数字是否为素数并执行以下步骤:
- 如果字符串number[]的长度为0,则返回false 。
- 使用函数修剪 修剪数字。
- 初始化一个长变量num并使用 parseLong函数将解析后的数字存储在其中。
- 如果num等于1,则返回false 。
- 如果num%2等于0且num不等于2,则返回false 。
- 如果num%3等于0且num不等于3,则返回false 。
- 使用变量i遍历范围[6, num 1/2 ]并执行以下步骤:
- 如果num%(i-1)或num%(i+1)中的任何一个等于0,则返回false 。
- 最后,返回true 。
- 定义一个函数DFS(int arr[], 字符串 ans)来查找所有可能的素数并执行以下步骤:
- 调用函数check(ans) ,如果函数返回true,则将此字符串ans添加到 HashSet H中。
- 使用变量i遍历范围[0, 10]并执行以下步骤:
- 如果arr[i]等于0,则继续迭代。
- 将i的值添加到字符串answer并将arr[i]的值减少1 。
- 调用函数DFS(arr, ans)以查找其他可能的回溯答案。
- 从字符串answer中删除i的值,并将arr[i]的值加上1 。
- 初始化一个大小为10的数组count[]以存储字符串S中每个数字的频率。
- 使用变量i迭代范围[0, N]并执行以下步骤:
- 将频率加1到字符串S中第i个索引中字符的数组count[] 。
- 调用函数DFS(count, “”)来查找所有可能的素数。
- 执行上述步骤后,打印 HashSet H的大小作为答案。
下面是上述方法的实现。
C++
#include
using namespace std;
unordered_set H;
// Function to check whether the
// number is prime or not
bool check(string number)
{
if (number.length() == 0) {
return false;
}
long num = stol(number);
// Condition for prime number
if (num == 1) {
return false;
}
if (num % 2 == 0 && num != 2) {
return false;
}
if (num % 3 == 0 && num != 3) {
return false;
}
// Iterate over the range [6, num]
for (int i = 6; i * i <= num; i += 6) {
if (num % (i - 1) == 0 || num % (i + 1) == 0) {
return false;
}
}
// Otherwisem return true
return true;
}
// Function to count the total number
// of prime numbers
void DFS(int arr[], string ans)
{
// Add it in the HashSet
if (check(ans)) {
H.insert(ans);
}
for (int i = 0; i <= 9; ++i) {
if (arr[i] == 0) {
continue;
}
// Use the number
ans = (ans + to_string(i));
// Decrease the number
arr[i]--;
// Perform the DFS Call
DFS(arr, ans);
ans = ans.substr(0, ans.length() - 1);
// Backtracking the frequency
arr[i]++;
}
}
// Driver Code
int main()
{
string number = "123";
int count[10];
for (int i = 0; i < 10; i++) {
count[i] = 0;
}
for (int i = 0; i < number.length(); i++) {
count[number[i] - '0']++;
}
H.clear();
DFS(count, "");
cout << H.size();
return 0;
}
// This code is contributed by maddler.
Java
// Java program for the above approach
import java.util.*;
public class GFG {
static HashSet H = new HashSet<>();
// Function to check whether the
// number is prime or not
static boolean check(String number)
{
if (number.length() == 0) {
return false;
}
number = number.trim();
long num = Long.parseLong(number);
// Condition for prime number
if (num == 1) {
return false;
}
if (num % 2 == 0 && num != 2) {
return false;
}
if (num % 3 == 0 && num != 3) {
return false;
}
// Iterate over the range [6, num]
for (int i = 6; i * i <= num; i += 6) {
if (num % (i - 1) == 0 || num % (i + 1) == 0) {
return false;
}
}
// Otherwisem return true
return true;
}
// Function to count the total number
// of prime numbers
static void DFS(int arr[], String ans)
{
// Add it in the HashSet
if (check(ans) == true) {
H.add(ans);
}
for (int i = 0; i <= 9; ++i) {
if (arr[i] == 0) {
continue;
}
// Use the number
ans += i;
// Decrease the number
arr[i]--;
// Perform the DFS Call
DFS(arr, ans);
ans = ans.substring(
0, ans.length() - 1);
// Backtracking the frequency
arr[i]++;
}
}
// Driver Code
public static void main(String[] args)
{
String number = "123";
int count[] = new int[10];
for (int i = 0; i < number.length(); ++i) {
count[number.charAt(i) - 48]++;
}
// Perform the DFS Traversal
DFS(count, "");
// Print the result
System.out.println(H.size());
}
}
Python3
H = set()
# Function to check whether the
# number is prime or not
def check(number):
if (len(number) == 0):
return False
num = int(number)
# Condition for prime number
if (num == 1):
return False
if (num % 2 == 0 and num != 2):
return False
if (num % 3 == 0 and num != 3):
return False
# Iterate over the range [6, num]
i = 6
while(i * i <= num):
if (num % (i - 1) == 0 or num % (i + 1) == 0):
return False
i = i + 6
# Otherwisem return true
return True
# Function to count the total number
# of prime numbers
def DFS(arr, ans):
# Add it in the HashSet
if (check(ans)):
H.add(ans)
for i in range(10):
if (arr[i] == 0):
continue
# Use the number
ans = (ans + str(i))
# Decrease the number
arr[i] -= 1
# Perform the DFS Call
DFS(arr, ans)
ans = ans[0: len(ans) - 1]
# Backtracking the frequency
arr[i]+=1
number = "123"
count = [0]*(10)
for i in range(10):
count[i] = 0
for i in range(len(number)):
count[ord(number[i]) - ord('0')] += 1
H.clear()
DFS(count, "")
print(len(H))
# This code is contributed by divyesh072019.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
static HashSet H = new HashSet();
// Function to check whether the
// number is prime or not
static bool check(String number)
{
if (number.Length == 0) {
return false;
}
number = number.Trim();
long num = long.Parse(number);
// Condition for prime number
if (num == 1) {
return false;
}
if (num % 2 == 0 && num != 2) {
return false;
}
if (num % 3 == 0 && num != 3) {
return false;
}
// Iterate over the range [6, num]
for (int i = 6; i * i <= num; i += 6) {
if (num % (i - 1) == 0 || num % (i + 1) == 0) {
return false;
}
}
// Otherwisem return true
return true;
}
// Function to count the total number
// of prime numbers
static void DFS(int []arr, String ans)
{
// Add it in the HashSet
if (check(ans) == true) {
H.Add(ans);
}
for (int i = 0; i <= 9; ++i) {
if (arr[i] == 0) {
continue;
}
// Use the number
ans += i;
// Decrease the number
arr[i]--;
// Perform the DFS Call
DFS(arr, ans);
ans = ans.Substring(
0, ans.Length - 1);
// Backtracking the frequency
arr[i]++;
}
}
// Driver Code
public static void Main(String[] args)
{
String number = "123";
int []count = new int[10];
for (int i = 0; i < number.Length; ++i) {
count[number[i] - 48]++;
}
// Perform the DFS Traversal
DFS(count, "");
// Print the result
Console.WriteLine(H.Count);
}
}
// This code contributed by shikhasingrajput.
Javascript
输出:
5
时间复杂度: O(9 N )
辅助空间: O(1)