Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 150′th ugly number.


METHOD 1 (Simple)

Thanks to Nedylko Draganov for suggesting this solution.

Algorithm:
Loop for all positive integers until ugly number count is smaller than n, if an integer is ugly than increment ugly number count.

To check if a number is ugly, divide the number by greatest divisible powers of 2, 3 and 5, if the number becomes 1 then it is an ugly number otherwise not.

For example, let us see how to check for 300 is ugly or not. Greatest divisible power of 2 is 4, after dividing 300 by 4 we get 75. Greatest divisible power of 3 is 3, after dividing 75 by 3 we get 25. Greatest divisible power of 5 is 25, after dividing 25 by 25 we get 1. Since we get 1 finally, 300 is ugly number.

Below is the simple method, with printing programme, which can print the ugly numbers:

int maxDivide(int num, int div)
{
while (num % div == )
{
num /= div;
}
return num;
} bool isUgly(int num)
{
num = maxDivide(num, );
num = maxDivide(num, );
num = maxDivide(num, );
return num == ? true:false;
} int getNthUglyNo(int n)
{
int c = ;
int i = ;
while (c < n)
{
if (isUgly(++i)) c++;
}
return i;
}
#include <vector>
using std::vector;
vector<int> getAllUglyNo(int n)
{
vector<int> rs;
for (int i = ; i <= n; i++)
{
if (isUgly(i)) rs.push_back(i);
}
return rs;
}

Dynamic programming:

Watch out:  We need to skip some repeated numbers, as commented out below.

Think about this algorithm, conclude as:

We caculate ugly numbers from button up, every new ugly number multiply 2,3,5 respectly would be a new ugly number.

class UglyNumbers
{
public:
int getNthUglyNo(int n, vector<int> &rs)
{
if (n < ) return n;
int n2 = , n3 = , n5 = ;
int i2 = , i3 = , i5 = ;
rs.resize(n, );
for (int i = ; i < n; i++)
{
int t = min(n2, min(n3,n5));
if (t == n2)
{
rs[i] = n2;
n2 = rs[++i2]*;
}
if (t == n3) //Watch out, maybe repeated numbers
{
rs[i] = n3;
n3 = rs[++i3]*;
}
if (t == n5) //Watch out, no else!
{
rs[i] = n5;
n5 = rs[++i5]*;
}
}
return rs.back();
}
};

Testing:

int main()
{
unsigned no = getNthUglyNo();
printf("ugly no. is %d \n", no);
vector<int> rs = getAllUglyNo();
for (auto x:rs) cout<<x<<" ";
cout<<endl; UglyNumbers un;
printf("Ugly no. is %d \n", un.getNthUglyNo(, rs));
for (auto x:rs) cout<<x<<" ";
cout<<endl; system("pause");
return ;
}

Geeks Interview Question: Ugly Numbers的更多相关文章

  1. lintcode :Ugly Numbers 丑数

    题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...

  2. 因子问题 I - Ugly Numbers

    题目: Ugly numbers are numbers whose only prime factors are 2, 3 or 5 . The sequence 1, 2, 3, 4, 5, 6, ...

  3. an interview question(1)

    声明:本文为博主原创文章,未经博主允许不得转载. 以下是英文翻译: warnning: Copyright!you can't reprint this blog when you not get b ...

  4. poj 1338 Ugly Numbers(丑数模拟)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338&q ...

  5. LeetCode OJ:Ugly Number II(丑数II)

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. 丑数(Ugly Numbers, UVa 136)

    丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...

  7. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  8. LeetCode OJ:Ugly Number(丑数)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  9. UVA - 136 Ugly Numbers (有关set使用的一道题)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...

随机推荐

  1. JavaScript高级编程II

         原文地址: http://www.onlamp.com/pub/a/onlamp/2007/08/23/advanced-javascript-ii.html?page=1 在前面的文章中, ...

  2. Android自定义属性、控件三步法

    第二步中layout-activity_main.xml 中自命名控件: xmlns:android="http://schemas.android.com/apk/res/android& ...

  3. CentOS6.3 Firefox安装FlashPlayer

    这段时间搞搞CentOS,我自己用的版本是CentOS6.3,基本上都差不多,过程都一样,主要说一下步骤 1.从Adoble官网下载FlashPlayer插件,下载地址:http://get.adob ...

  4. 转 - Web新人(偏前端)应该怎样学习(个人观点,勿喷)

    我自己是会计专业,转行自学web的,学习有一两年了,也还是新人一个,只不过不是那种超级“新”的,所以有什么话说得不对,请轻喷.欢迎大家来和我交流. 1.我能不能转行学web? 能不能学web这个不是别 ...

  5. ClickOnce发布后不能安装

    当在internet发布用ClickOnce打包的客户端程序时,遇到ClickOnce启动后出错,错误信息如下: + Downloading https://xxxxx/Deploy/pc/Boote ...

  6. ICSharpCode.SharpZipLib实现压缩解压缩

    最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...

  7. raw和字符串的转换。

    hextoraw():十六进制字符串转换为raw: rawtohex():将raw串转换为十六进制: select hextoraw('gggggg') from dual

  8. java虚拟机涉及内存溢出

    Java语言写的代码是.java文件,它会被特定程序编译(javac.exe,它会被Eclipse之类的IDE调用)成字节码(bytecode),字节码不能直接在CPU上运行,需要另一个程序读取并执行 ...

  9. MySQL的truncate table 和source 命令

    1. truncate table XXX     在测试时,我很讨厌某表的主键一直自增长下去,总觉得从1开始最舒服,^_^,truncate table 就可以帮我,相比delete from 来说 ...

  10. ThinkPHP实现RBAC

    RBAC: role base access control   基于角色的用户访问权限控制 不同人员登录系统要显示不同的菜单项目 1.传统方式权限设置: 具体操作权限与用户直接联系: