使用给定数字的数字构成所有质数


任何大于1的数,如果它不能写成两个较小自然数的乘积(除了1和它本身),则称其为质数。例如,5是质数,因为它的唯一乘积形式1×5和5×1都包含5。正如素数定理所述,素数在数论中起着至关重要的作用,素数定理断言任何大于1的自然数要么本身是素数,要么可以表示为素数的唯一乘积。该定理突出了素数在数学领域的重要性。

问题陈述

目标是确定可以使用长度为N的字符串S中存在的数字生成的不同的质数的计数。

示例

输入

S =17

输出

3

解释

可以使用S的数字生成的质数是

7 17 71

输入

S =1234

输出

15

解释

可以使用S的数字生成的质数是

23 31 41 43 241 421 431 1243 1423 2143 2341 4231

输入

S =5

输出

1

解释

可以使用S的数字生成的质数是

5

解决方案方法

一种可能的方法是识别可以使用给定数字中存在的数字构造的所有数字。我们迭代所有可能的数字组合,并检查每个组合是否为质数。

算法

  • 将输入数字读取为字符串。

  • 初始化一个大小为10的数字数组,以存储输入数字中每个数字的频率。对字符串中的字符进行迭代,然后更新遇到的每个数字的出现次数。

  • 初始化一个空的无序质数集合,以存储唯一的质数字符串。

  • 创建一个名为“is_prime”的函数,该函数接受整数输入“num”,如果“num”是质数,则返回true;否则,返回false。

    • 如果num小于2,则返回false。

    • 在2到“num”的平方根范围内进行迭代,并验证“num”是否能被此范围内的任何数字整除。如果找到除数,则返回false。

    • 如果num不能被范围内的任何数字整除,则返回true。

  • 定义一个递归函数generate_primes,它接受数字数组、整数索引、整数num和无序质数集作为输入。

    • 如果索引等于10,则返回。

    • 对于0到9范围内的每个整数“i”,迭代执行以下步骤

    • 如果digits[i]等于0,则继续迭代。

    • 通过将i连接到num的末尾来计算新的数字new_num。

    • 如果new_num是质数,则将其字符串表示形式添加到集合primes中。

    • 递减digits[i]。

    • 使用digits、index+1、new_num和primes作为输入递归调用generate_primes。

    • 递增digits[i]。

  • 使用digits、0、0和primes作为输入调用generate_primes,以使用输入数字的数字生成所有可能的质数字符串。

  • 打印集合primes的大小作为输出。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例:C++程序

给定一个正整数作为输入,该程序旨在识别可以使用该数字中存在的数字生成的所有质数。它通过系统地迭代所有可能的数字组合并验证每个组合的质数来实现这一点。该程序的输出是使用输入数字的数字可以形成的不同质数的计数。

示例

Open Compiler
#include <iostream> #include <unordered_set> using namespace std; // Function to check primality of a number bool is_prime(int num) { // Since 2 is the smallest prime number, if the number is less than 2, return false if (num < 2) { return false; } // Verify whether the number is divisible by any integer within the range of 2 to the square root of the number. for (int i = 2; i*i <= num; i++) { if (num % i == 0) { return false; } } // If the number is not divisible by any number from 2 to the square root of the number, it is prime return true; } // Recursive function to generate all prime numbers using the given digits void generate_primes(int digits[], int index, int num, unordered_set<string>& primes) { // If all digits have been used, return if (index >= 10) { return; } // Generate all possible combinations of the digits for (int i = 0; i < 10; i++) { // If the digit has already been used up, skip it if (digits[i] == 0) { continue; } // Append the current digit to the number being generated int new_num = num*10 + i; // If the new number is prime, add it to the set of primes if (is_prime(new_num)) { primes.insert(to_string(new_num)); } // Decrement the count of the current digit in the digits array digits[i]--; // Recursively generate all primes using the remaining digits and the new number generate_primes(digits, index+1, new_num, primes); // Increase the occurrence count of the current digit within the digits array for the purpose of backtracking. digits[i]++; } } int main() { // Input number whose digits are used to generate primes string number = "17"; cout << "Given number: " << number << endl; // An array is utilized to store the frequency count of each digit present in the input number int digits[10] = {0}; // Count the occurrences of each digit in the input number for (char c : number) { digits[c-'0']++; } // Set to store the unique prime numbers generated unordered_set<string> primes; // Generate all prime numbers using the digits and add them to the set of primes generate_primes(digits, 0, 0, primes); // Print the number of unique prime numbers generated cout << "Number of unique prime numbers that can be formed using digits of a given number: "; cout << primes.size() << endl; return 0; }

输出

Given number: 17
Number of unique prime numbers that can be formed using digits of a given number: 3

当程序提供输入数字17时,输出将为3,这表示可以使用数字17中存在的数字构造三个质数,分别是7、17和71。

为了解释程序的工作原理,它首先初始化一个大小为10的数组,以存储输入数字中每个数字的频率。在这种情况下,数组将为[0, 1, 0, 0, 0, 0, 0, 1, 0, 0],因为数字17中有一个7和一个1。

然后,程序使用递归函数生成所有可能的数字组合,并检查每个组合是否为质数。在这种情况下,该函数将生成数字7、17和71。

生成的全部数字7、17和71都是质数。因此,程序产生一个输出,指示可以使用输入数字中找到的数字生成的不同的质数的总数,在这种情况下为3。

时间和空间复杂度分析

时间复杂度:O(10^n * sqrt(n))

is_prime(): O(sqrt(n))

generate_primes(): O(10^n * sqrt(n)),其中变量“n”表示输入数字中存在的数字总数。这是因为,对于数字中的每个数字,我们有10个可能的选项(0-9),我们重复此过程n次。因此,我们生成的可能的组合总数为10^n。然后,我们使用is_prime函数检查数字的质数性,这需要O(sqrt(num))时间。

空间复杂度:O(10^n + p)

generate_primes()函数占用的辅助内存由于递归调用而为O(10^n)。无序集合对象primes也影响空间复杂度,它是O(p),其中p是生成的质数的数量。

结论

本文探讨了一种递归方法,用于计算可以使用表示为字符串的给定数字的数字生成的质数的总数。我们讨论了问题的概念、解决方案方法、算法、C++程序以及时间和空间复杂度。

更新于:2023年8月27日

261 次浏览

启动您的职业生涯

完成课程后获得认证

开始
广告