I like that Python code, and for three digits numbers it is just fine.

If the number could be in a larger range, factoring out digits starting 
with larger digits should give the correct sequence, in reverse. If the 
number has a prime factor larger than 9, there is no solution.

// Returns the smallest number such that the product of the digits is n.
// Returns -1 if no such number exists.
int productOfDigits(int n)
{
   int result=0;
   int mlt = 1;
   if (n == 0) return 0;
   if (n == 1) return 1;
   for(int i = 9; i > 1; --i)
   {
      while(n % i == 0)
      {
         result += mlt*i;
         mlt *= 10;
         n /= i;
         if (n == 1) return result;
      }
   }
   return -1;
}


On Wednesday, February 12, 2014 10:23:52 AM UTC-5, Shashwat Anand wrote:
>
> Since the limit is so small, won't a simple bruteforce do ?
> Just run a loop from [100, 1000) and return the minimum.
>
> Here is a quick python code I wrote.
>
>  >>> N = 24
> >>> min (i for i in range (100, 1001) if int (str (i) [0]) * int (str (i) 
> [1]) * int (str (i) [2]) == N)
> 138
> >>> N = 36
> >>> min (i for i in range (100, 1001) if int (str (i) [0]) * int (str (i) 
> [1]) * int (str (i) [2]) == N)
> 149
> >>> N = 100
> >>> min (i for i in range (100, 1001) if int (str (i) [0]) * int (str (i) 
> [1]) * int (str (i) [2]) == N)
> 455
>
>
> On Wed, Feb 12, 2014 at 8:45 PM, atul anand <[email protected]<javascript:>
> > wrote:
>
>> Given a number N, find the smallest 3 digits number  such that product of 
>> its digits is equal to N.
>>
>> For Eg:-
>>
>> for input 24 , output :138 (1*3*8 = 24)
>> for input 36 , output :149 (1*4*9 = 36)
>> for input 100 , output : 455 (4*5*5 = 100)
>>  
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Algorithm Geeks" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].

Reply via email to