Geeks Interview Question: Ugly Numbers
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的更多相关文章
- lintcode :Ugly Numbers 丑数
题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...
- 因子问题 I - Ugly Numbers
题目: Ugly numbers are numbers whose only prime factors are 2, 3 or 5 . The sequence 1, 2, 3, 4, 5, 6, ...
- an interview question(1)
声明:本文为博主原创文章,未经博主允许不得转载. 以下是英文翻译: warnning: Copyright!you can't reprint this blog when you not get b ...
- poj 1338 Ugly Numbers(丑数模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338&q ...
- LeetCode OJ:Ugly Number II(丑数II)
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- 丑数(Ugly Numbers, UVa 136)
丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...
- UVA.136 Ugly Numbers (优先队列)
UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...
- LeetCode OJ:Ugly Number(丑数)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- 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, ...
随机推荐
- Linux负载均衡软件LVS之二(安装篇)[转]
Linux负载均衡软件LVS之二(安装篇) 2011-04-26 16:01:47 标签:lvs安装配置 linux lvs 休闲 linux高可用 原创作品,允许转载,转载时请务必以超链接形式标明文 ...
- Laravel 4 Quick Tip: Custom Error Pages
App::error(function($exception, $code) { switch ($code) { case 403: return Response::view('errors.40 ...
- COM编程入门第一部分——什么是COM,如何使用COM
本文的目的是为刚刚接触COM的程序员提供编程指南,并帮助他们理解COM的基本概念.内容包括COM规范简介,重要的COM术语以及如何重用现有的COM组件.本文不包括如何编写自己的COM对象和接口. CO ...
- [Redux] Avoiding Array Mutations with concat(), slice(), and ...spread
For Redux, you cannot use mutable methods like push, splice. Need to use immutable methods such as c ...
- cocos2d 高仿doodle jump 无源代码
1. 游戏视频 主角眼熟吗?没错,上次跑酷游戏中的"30"来Jump了,有三种道具.主角光环,竹蜻蜓.翅膀: 有两种怪物,螃蟹和鸟: 有5种板子.点击屏幕,30会把它的嘴巴3给发射 ...
- 小学生之Map集合框架的使用
Map用于保存具有映射关系的数据(key-vlaue).Map的key不允许重复,即同一个Map对象的任何两个key通过equals方法比较总是返回false Map中包含了一个keySet()方法, ...
- (@DBRef)spring-data-mongodb
@DBRef用在哪些地方 已知的有 @DBRefprivate Shop product; @DBRefprivate List<Account> accounts; 如果不加@DB ...
- Android Studio中新建项目时Your android sdk is out of date or is missing templates的解决办法
在Android Studio中新建项目时出现了以下问题:Your android sdk is out of date or is missing templates. Please ensure ...
- mysql文件导入到数据库load data infile into table 的使用例子
load data infile "C:/Users/Administrator/Desktop/1.txt"into table 要一个已经存的表名 字段默认用制表符隔开 文件 ...
- Something About Variable
CONTENT(目录) 前言 Variable declearation:three rules you can break 1.Don't set var stat ...