Find the smallest number whose digits multiply to a given number n

Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ‘n’. The result ‘p’ should have minimum two digits.

Examples:

Input:  n = 36
Output: p = 49
// Note that 4*9 = 36 and 49 is the smallest such number

Input:  n = 100
Output: p = 455
// Note that 4*5*5 = 100 and 455 is the smallest such number

Input: n = 1
Output:p = 11
// Note that 1*1 = 1

Input: n = 13
Output: Not Possible

For a given n, following are the two cases to be considered.
Case 1: n < 10 When n is smaller than n, the output is always n+10. For example for n = 7, output is 17. For n = 9, output is 19.

Case 2: n >= 10 Find all factors of n which are between 2 and 9 (both inclusive). The idea is to start searching from 9 so that the number of digits in result are minimized. For example 9 is preferred over 33 and 8 is preferred over 24.
Store all found factors in an array. The array would contain digits in non-increasing order, so finally print the array in reverse order.

 1     public static int findSmallest(int n){
 2         if(n < 10) return 10 + n;
 3         int out = 0;
 4         int pos = 0;
 5         for(int i = 9; i > 1; i --){
 6             while(n % i == 0){
 7                 n /= i;
 8                 out += i * Math.pow(10, pos);
 9                 pos ++;
10             }
11         }
12         return n ==1 ? out : -1;
13     }

Find the smallest number whose digits multiply to a given number n

时间: 2024-10-25 23:39:12

Find the smallest number whose digits multiply to a given number n的相关文章

[LeetCode]1295. Find Numbers with Even Number of Digits

Given an array nums of integers, return how many of them contain an even number of digits. Example 1: Input: nums = [12,345,2,6,7896]Output: 2Explanation: 12 contains 2 digits (even number of digits). 345 contains 3 digits (odd number of digits). 2 c

ExtJS学习-----------Ext.Number,ExtJS对javascript中的Number的扩展

关于ExtJS对javascript中的Number的扩展,可以参考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 下面对其中的部分方法进行介绍: (1)constrain constrain( Number number, Number min, Number max ) : Number 检查给定的数值是否在约束的范围内. If the number is already within the 如果再范围内就返

&#39;Invalid update: invalid number of rows in section xx. The number of rows contained in an existing section after the update (xxx)...

'Invalid update: invalid number of rows in section 5.  The number of rows contained in an existing section after the update (299) must be equal to the number of rows contained in that section before the update (276), plus or minus the number of rows

[AtCoder arc090F]Number of Digits

Description 题库链接 记 \(d\) 在十进制下的位数为 \(f(d)\) .给出询问 \(S\) ,求有多少对 \((l,r)\) 使得 \[\sum_{i=l}^r f(i)=S\] \(1\leq S\leq 10^8\) Solution 颓了题解... 注意到当数字越大时 \(f(r)-f(l)\) 会越小. 分两种情况讨论: \(f(l)\leq 7\) ,这时可以用尺取法来做,可以发现它的右界为 \(10^7+\frac{10^8}{8}=25500000\) : \(

leetcode 202. Happy Number 判断一个数是否是“Happy Number” ---------- java

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the

理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)

原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ from:https://blog.csdn.net/a19881029/article/details/38091243 如果你正在读这篇文章,很可能你对TCP“非著名”的“三次握手”或者说“SYN,SYN/ACK,ACK”已经很熟悉了.不幸的是,对很多人来说,对TCP的学习就仅限于此了.尽管年代久远,TCP仍

Round #427 B. The number on the board(Div.2)

Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change. You h

实现sprintf--浮点数打印字符串

亲爱的程序猿们,你们肯定都用过printf吧?你们知道,看起来理所当然的简单的printf,实际上是个难倒众多计算机科学家的难题吗?直到1971年,才有我们的毒师Mr.White科学家(Jon White)解决这个问题,直到1990年,我们的Mr.White才正式发表这算法的最终版本,Dragon4, 在随后到最近的几十年来,语言上的各种浮点数打印字符串都是用Dragon4算法,其他人的研究都只是对这个算法修修补补,直到Grisu算法的出现.Grisu算法由Florian Loitsch发表,6

算法中O字符解释

出处:stackoverflow 伯乐在线有同志翻译了一下:http://blog.jobbole.com/55184/ Big O complexity can be visualized with this graph: The simplest definition I can give for Big-O notation is this: Big-O notation is a relative representation of the complexity of an algori