Pandigital Product


We are given two numbers and our task is to find out whether the given number is obtained by multiplying two other numbers such that all three numbers together constitute a 9-digit pandigital number.

In other words, it can be said that we have to find out whether the given number is pandigital after combining it with two other numbers which result in the original number on multiplication.

We can have many such cases where we will get multiple solutions for this problem, to get the best time complexity, we will simply print the first ever solution found and stop our iteration process.

Solution: Let us first discuss what is a pandigital number −

A number with n digits can be called a pandigital number if and only if it uses all the digits from 1 to n only once. i.e., The number can be represented as a permutation of all the digits from 1 to n using one digit only once.

E.g., 6745312 is a 7-digit pan digit number as it uses all the numbers from 1 to 7

Let us now understand the problem using a few examples −

Given Number: 7254
Result obtained: Yes, the condition is true

As we know, 7254 can be represented as a product of 39 and 186.

And after combining 39, 186 and 7254, we get 391867254, which contain all the digits from 1 to 9 using each digit only once i.e., it is a pandigital number of 9 digits.

Given Number: 6952
Result obtained: Yes, the condition is true

Approach

Now, let us discuss our approach to solve this problem −

We will first figure out all the pairs of numbers which on multiplication results in the given number to be checked for. Then for each pair of possible solutions, we will create a string and store all three numbers (the original number and its two factors which result in the number on multiplication).

Let us now look for the working algorithm for our solution.

  • Step 1 − Iterate over a loop to check for all the pairs of factors for the number.

  • Step 2 − For each part of the factor, we will create a string containing the original number and the two factors.

  • Step 3 − Sort the string so formed using sort() function.

  • Step 4 − Now we will create another string “123456789”

  • Step 5 − Compare both the strings and return true if both are the same.

Example

The code for this approach is given below −

#include <bits/stdc++.h>
using namespace std;

// this function checks whether the given string consist of pandigital numbers
bool Is_Pandigital_product (string Original_string) {
   if ( Original_string.length() != 9 ) {
      return false;
   }
   char c[Original_string.length()];
   strcpy(c, Original_string.c_str());
   sort(c, c + Original_string.length());
   string new_string = c;
   if(new_string.compare("123456789") == 0) // comparing both the strings
   {
      return true;
   } else {
      return true;
   }
}
bool PandigitalProduct_1_9(int num)
// This function iterates over a loop till Sqrt(n) and searches for factors of given input.
// for each factor, this loop calls for Is_Pandigital_product function
{
   for (int Iterator = 1; Iterator * Iterator <= num; Iterator++)
      if (num % Iterator == 0 && Is_Pandigital_product(to_string(num) + to_string(Iterator) + to_string(num / Iterator)))
   return true; //Returns true if found pandigital number
   return false;
}
int main() {
   int num = 1234;
   if (PandigitalProduct_1_9(num) == true)
      cout << "yes the number " << num << " is a pandigital product";
   else
      cout << "no the number " << num <<" is not a pandigital product";
    return 0;
}

Output

yes the number 1234 is a pandigital product

Time Complexity − Since we have used a single loop which iterates from 1 to sqrt(n), the time complexity for this solution would be O(N^1/2)

Space complexity − Since the code doesn’t require any additional memory, the space complexity is linear i.e., O(1).

In this article, we have studied about what is a pandigital number and an efficient approach to know whether the given number, its factors(in pairs) which on multiplication gives the same number after combining in a string gives a 9 digit pandigital number.

Updated on: 11-Apr-2023

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements