Home  >  Article  >  Backend Development  >  All-round digital products

All-round digital products

WBOY
WBOYforward
2023-09-19 13:09:10946browse

All-round digital products

Given two numbers, our task is to find out whether the given number is obtained by multiplying the other two numbers such that all three numbers together form a 9 Digits.

In other words, it can be said that we have to find out whether a given number when combined with two other numbers forms a multiplication operation to get the full number of the original number.

We may come across many situations where we will get multiple solutions to the problem and to get the best time complexity we will simply print the first solution found and stop the iterative process.

Solution: First let’s discuss what is a fully numeric number -

An n-digit number is called a pandigit if and only if it uses all the digits from 1 to n exactly once. That is, the number can be represented as a permutation of all numbers from 1 to n using only one digit at a time.

For example, 6745312 is a 7-digit pan number because it uses all digits from 1 to 7

Now let us understand this problem with a few examples -

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

As we all know, 7254 can be expressed as the product of 39 and 186.

Adding 39, 186 and 7254, we get 391867254, which contains all numbers from 1 to 9. Each number is only used once, that is, it is a full number composed of 9 numbers.

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

method

Now, let’s discuss ways to solve this problem−

We first check to find all pairs of numbers whose product is equal to the given number. Then for each possible pair of solution numbers, we will create a string and store all three numbers (the original number and the two factors that cause the product to be that number).

Now let's find a working algorithm for our solution.

  • Step 1 - Iterate the loop to check all pairs of factors for that number.

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

  • Step 3 - Use the sort() function to sort the formed string.

  • Step 4 - Now we will create another string "123456789"

  • Step 5 - Compare the two strings and return true if they are the same.

Example

The code for this method is as follows -

#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 are using a single loop iterating from 1 to sqrt(n), the time complexity of this solution will be O(N^1/2)

Space Complexity - Since the code does not require any additional memory, the space complexity is linear, i.e. O(1).

In this article, we look at what are all-numeric numbers and an efficient way to tell whether a given number and its factors (pairs) result in a 9-digit number when combined into a string after multiplication All numbers.

The above is the detailed content of All-round digital products. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete