search
HomeBackend DevelopmentC++All-round digital products

All-round digital products

Sep 19, 2023 pm 01:09 PM
Internet of thingsVirtual Realitysmart home

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. If there is any infringement, please contact admin@php.cn delete
Using XML in C  : A Guide to Libraries and ToolsUsing XML in C : A Guide to Libraries and ToolsMay 09, 2025 am 12:16 AM

XML is used in C because it provides a convenient way to structure data, especially in configuration files, data storage and network communications. 1) Select the appropriate library, such as TinyXML, pugixml, RapidXML, and decide according to project needs. 2) Understand two ways of XML parsing and generation: DOM is suitable for frequent access and modification, and SAX is suitable for large files or streaming data. 3) When optimizing performance, TinyXML is suitable for small files, pugixml performs well in memory and speed, and RapidXML is excellent in processing large files.

C# and C  : Exploring the Different ParadigmsC# and C : Exploring the Different ParadigmsMay 08, 2025 am 12:06 AM

The main differences between C# and C are memory management, polymorphism implementation and performance optimization. 1) C# uses a garbage collector to automatically manage memory, while C needs to be managed manually. 2) C# realizes polymorphism through interfaces and virtual methods, and C uses virtual functions and pure virtual functions. 3) The performance optimization of C# depends on structure and parallel programming, while C is implemented through inline functions and multithreading.

C   XML Parsing: Techniques and Best PracticesC XML Parsing: Techniques and Best PracticesMay 07, 2025 am 12:06 AM

The DOM and SAX methods can be used to parse XML data in C. 1) DOM parsing loads XML into memory, suitable for small files, but may take up a lot of memory. 2) SAX parsing is event-driven and is suitable for large files, but cannot be accessed randomly. Choosing the right method and optimizing the code can improve efficiency.

C   in Specific Domains: Exploring Its StrongholdsC in Specific Domains: Exploring Its StrongholdsMay 06, 2025 am 12:08 AM

C is widely used in the fields of game development, embedded systems, financial transactions and scientific computing, due to its high performance and flexibility. 1) In game development, C is used for efficient graphics rendering and real-time computing. 2) In embedded systems, C's memory management and hardware control capabilities make it the first choice. 3) In the field of financial transactions, C's high performance meets the needs of real-time computing. 4) In scientific computing, C's efficient algorithm implementation and data processing capabilities are fully reflected.

Debunking the Myths: Is C   Really a Dead Language?Debunking the Myths: Is C Really a Dead Language?May 05, 2025 am 12:11 AM

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

C# vs. C  : A Comparative Analysis of Programming LanguagesC# vs. C : A Comparative Analysis of Programming LanguagesMay 04, 2025 am 12:03 AM

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

Building XML Applications with C  : Practical ExamplesBuilding XML Applications with C : Practical ExamplesMay 03, 2025 am 12:16 AM

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

XML in C  : Handling Complex Data StructuresXML in C : Handling Complex Data StructuresMay 02, 2025 am 12:04 AM

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools