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. XTU1199:Number Game

    题目描写叙述 给你一个有N个数的集合S和一个数X,推断是否存在S的一个子集,子集里的数的最小公倍数正好是X. 输入 第一行是数据组数T. 接下来有多组数据,每组数据包括两行: 第一行有2个数N和X,1 ...

  2. msxml 操作xml

    1.简介 在.NET平台,微软为C#或托管C++程序员提供了丰富的类库,用以支持各种需求,其中就有对XML文件操作的丰富的类.例如XMLDocument, XmlElement等.但是C++标准库中并 ...

  3. java图片处理工具类

    直接上代码: package com.zxd.tool; /** * Created by zhang on 14-3-1. * 图片的常用操作类 */ import java.awt.AlphaCo ...

  4. Java基础知识强化96:Calendar类之获取任意年份的2月有多少天的案例

    1. 分析: (1)键盘录入任意的年份 (2)设置日历对象的年月日            年:就是(1)输入的数据            月:是2(3月份)            日:是1  (3)把 ...

  5. Java基础知识强化91:DateFormat类之DateFormat实现日期和字符串的相互转换

    1. DateFormat类概述: DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateFormat 2.  ...

  6. HDU -1864最大报销额(01背包)

    这道题属于简单的01背包,但是背包问题还算简单,就是前面的细节处理的时候要注意,题意大致说了三条限制吧 1. 只有a, b, c 三种类型的发票可以报销,其它的一律不报销 2. 物品单项的报销额不超过 ...

  7. java执行命令行

    List<String> command = new ArrayList<String>(); command.add("ping"); ProcessBu ...

  8. 结束指定Activity实例代码

    开通博客两个多月了,一直在看你们的文章 终于发觉伸手党真的很可耻.. 于是就随便写了个Demo来结束伸手党生涯~ Demo很简单:结束指定Activity... 不过也是我的一个小心意嘛.. 不要责怪 ...

  9. thinkphp基础入门(1)

    ThinkPHP目录如下,Application顾名思义就是应用的意思(我们的代码放在这里),Public就是公共文件的意思(主要放JS CSS 等前端资源文件),ThinkPHP文件是框架的核心包( ...

  10. switch case加条件语句(非等值) php

    <?php $s_level=rand(1,6); echo $s_level.'<br/>'; switch(true){ case $s_level<3 : echo 'l ...