Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(beginner here)

My task says I have to find the smallest number whose digits multiply to a given number. If such number cannot be made output "-1"

Example:

10 = 25       2*5=10<br />
13 = -1  <br />
5 = 5     <br />
100 = 455     4*5*5 = 100

input is a single number n;
0 <= n <= 10^9


My code seems to work fine, but whenever I try to upload it to a contest website my code doesn't pass all the test cases. All test cases are hidden so I don't know the specific test case my code fails.

So I wanted to ask for help, I want to know how can I find that specific test case or even better help me find that test case.

My code is based on code from these websites:

Smallest number k such that the product of digits of k is equal to n - GeeksforGeeks[^]

Find the smallest number whose digits multiply to a given number n - GeeksforGeeks[^]

My code:

#include <iostream>
#include <stack>
using namespace std;

    // function to find smallest number k such that
    // the product of digits of k is equal to n
    long long int smallestNumber(long long int n)
    {
        // if 'n' is a single digit number, then
        // it is the required number
        if (n >= 0 && n <= 9)
            return n;

        // stack the store the the digits
        stack<long long int> digits;

        // repeatedly divide 'n' by the numbers 
        // from 9 to 2 until all the numbers are 
        // used or 'n' > 1
        for (long long int i = 9; i >= 2 && n > 1; i--)
        {
            while (n % i == 0)
            {
                // save the digit 'i' that divides 'n'
                // onto the stack
                digits.push(i);
                n = n / i;
            }
        }

        // if true, then no number 'k' can be formed 
        if (n != 1)
            return -1;

        // pop digits from the stack 'digits'
        // and add them to 'k'
        long long int k = 0;
        while (!digits.empty())
        {
            k = k * 10 + digits.top();
            digits.pop();
        }

        // required smallest number
        return k;
    }

    // Driver program to test above
    int main()
    {
        long long int n;//number i want to convert
        cin >> n;
        cout << smallestNumber(n);
        return 0;
    }

Thank you.

What I have tried:

I tried numbers from 1 to 1000000 and they all seem to work fine
Posted
Updated 19-May-18 10:38am
v3
Comments
Rick York 20-May-18 13:12pm    
Note that the two requirements you linked to are different from each either. In one of them, one times a prime number is allowed. In the other it is not. I have seen this problem posted many, many times and I have only seen this one case where one times the prime number is allowed.

1 solution

Quote:
My code seems to work fine, but whenever I try to upload it to a contest website my code doesn't pass all the test cases.

Because your code is wrong !
For n=12, your answer is 223 when the best answer is 26.

Where do you try to upload your code ?
Quote:
So I wanted to ask for help, I want to know how can I find that specific test case or even better help me find that test case.

Short answer: you don't.
Test cases are kept secret on purpose.
This avoid submissions of program that just contain a list of answers for the specific test cases, and no real algorithm that solve the problem.
 
Share this answer
 
v4
Comments
Member 13834999 19-May-18 14:22pm    
What compiler are you using ? Because if I enter n=12 I get 26 (visual studio 2017)
Patrice T 19-May-18 14:28pm    
oops my bad :)
I read your code too fast.
Member 13834999 19-May-18 16:01pm    
I upload to a private university system
Patrice T 19-May-18 16:07pm    
You need to list the exact requirement if you want us to find what is wrong in your code.
Member 13834999 19-May-18 16:35pm    
the only requirement to this is 0 <= n <= 10^9 and everything I said, nothing else

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900