1.Description
Ugly Number
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
2.Solution
1.Description
AddDigits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
Hint:
- A naive implementation of the above process is trivial. Could you come up with other methods?
- What are all the possible results?
- How do they occur, periodically or randomly?
- You may find this Wikipedia article useful.
本文介绍如何编写程序检查一个数是否为丑数,即其质因数仅包含2、3、5的正整数,并探讨了如何将一个非负整数的所有位相加直至结果仅剩一位数的方法。提供了两种问题的解决方案。
712

被折叠的 条评论
为什么被折叠?



