Count all prime numbers that can be formed using digits of a given number


Any number larger than 1 is said to be prime if it cannot be written as the product of two smaller natural numbers except 1 and the number itself. For instance, 5 is prime since its only product forms, 1 5 and 5 1, both involve 5. Primes play a crucial role in number theory as stated by the prime number theorem, which asserts that any natural number greater than 1 is either a prime itself or can be expressed as a unique product of prime numbers. This theorem highlights the significance of prime numbers in the realm of mathematics.

Problem Statement

The objective is to ascertain the count of distinct prime numbers that can be generated by utilizing the digits present in a string S, which has a length of N.

Sample Examples

Input

S = “17”

Output

3

Explanation

The prime numbers that can be generated from the digits of S are

7 17 71

Input

S = “1234”

Output

15

Explanation

The prime numbers that can be generated from the digits of S are

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

Input

S = “5”

Output

1

Explanation

The prime numbers that can be generated from the digits of S are

5

Solution Approach

One potential approach involves identifying all the numbers that can be constructed by utilizing the digits present in the given number. We iterate over all possible combinations of digits and check if each combination is prime.

Algorithm

  • Read the input number as a string.

  • Initialize an array of digits of size 10 to store the frequency of each digit in the input number. Perform an iteration over the characters in the string and subsequently update the occurrence count of each digit encountered.

  • Initialize an empty unordered set of primes to store the unique prime number strings.

  • Create a function called "is_prime" which accepts an integer input "num" and returns true if "num" is a prime number; otherwise, it returns false.

    • If num is less than 2, return false.

    • Perform an iteration over the range from 2 to the square root of "num" and verify whether "num" is divisible by any number within this range. If a divisor is found, return false.

    • If num is not divisible by any number in the range, return true.

  • Define a recursive function generate_primes that takes an array of digits, an integer index, an integer num, and the unordered set of primes as input.

    • If the index is equal to 10, return.

    • For each integer "i" within the range of 0 to 9, execute the following steps iteratively:

    • If digits[i] is equal to 0, continue the iteration.

    • Compute the new number new_num by concatenating i to the end of num.

    • If new_num is prime, add its string representation to the set primes.

    • Decrement digits[i].

    • Call generate_primes recursively with digits, index+1, new_num, and primes as input.

    • Increment digits[i].

  • Call generate_primes with digits, 0, 0, and primes as input to generate all possible prime number strings using the digits of the input number.

  • Print the size of the set primes as the output.

Example: C++ Program

Given a positive integer as input, this program aims to identify all the prime numbers that can be generated using the digits present in that number. It accomplishes this by systematically iterating through all possible combinations of digits and verifying the primality of each combination. The program provides the output as the count of distinct prime numbers that can be formed using the digits of the input number.

Example

#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;
}

Output

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

When the program is provided with the input number 17, the output will be 3, signifying that there are three prime numbers that can be constructed using the digits present in the number 17, those are 7, 17, and 71.

To explain how the program works, it first initializes an array of size 10 to store the frequency of each digit in the input number. In this case, the array would be [0, 1, 0, 0, 0, 0, 0, 1, 0, 0], since there is one 7 and one 1 in the number 17.

The program then uses a recursive function to generate all possible combinations of digits and checks if each combination is prime. In this case, the function would generate the numbers 7, 17, and 71.

All the numbers generated, 7, 17, and 71 are prime numbers. Hence, the program produces an output indicating the overall count of distinct prime numbers that can be generated by utilizing the digits found within the input number, which in this case is 3.

Time and Space Complexity Analysis

Time Complexity: O(10^n * sqrt(n))

is_prime(): O(sqrt(n))

generate_primes(): O(10^n * sqrt(n)), where the variable "n" represents the total count of digits present in the input number. This is because, for each digit in the number, we have 10 possible choices (0-9) and we repeat this process n times. Therefore, the total number of possible combinations we generate is 10^n. We then check the primality of a number, which takes O(sqrt(num)) time using the is_prime function.

Space Complexity: O(10^n + p)

Auxiliary memory taken by generate_primes() function is O(10^n) due to the recursive calls. The unordered_set object primes also contribute to the space complexity, which is O(p) where p is the number of prime numbers generated.

Conclusion

The article explores a recursive method for calculating the total count of prime numbers that can be generated using the digits from a given number represented as a string. We discuss the concept of the problem, solution approach, algorithm, C++ program along with the time and space complexity.

Updated on: 27-Aug-2023

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements