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:

  1. int maxDivide(int num, int div)
  2. {
  3. while (num % div == )
  4. {
  5. num /= div;
  6. }
  7. return num;
  8. }
  9.  
  10. bool isUgly(int num)
  11. {
  12. num = maxDivide(num, );
  13. num = maxDivide(num, );
  14. num = maxDivide(num, );
  15. return num == ? true:false;
  16. }
  17.  
  18. int getNthUglyNo(int n)
  19. {
  20. int c = ;
  21. int i = ;
  22. while (c < n)
  23. {
  24. if (isUgly(++i)) c++;
  25. }
  26. return i;
  27. }
  28. #include <vector>
  29. using std::vector;
  30. vector<int> getAllUglyNo(int n)
  31. {
  32. vector<int> rs;
  33. for (int i = ; i <= n; i++)
  34. {
  35. if (isUgly(i)) rs.push_back(i);
  36. }
  37. return rs;
  38. }

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.

  1. class UglyNumbers
  2. {
  3. public:
  4. int getNthUglyNo(int n, vector<int> &rs)
  5. {
  6. if (n < ) return n;
  7. int n2 = , n3 = , n5 = ;
  8. int i2 = , i3 = , i5 = ;
  9. rs.resize(n, );
  10. for (int i = ; i < n; i++)
  11. {
  12. int t = min(n2, min(n3,n5));
  13. if (t == n2)
  14. {
  15. rs[i] = n2;
  16. n2 = rs[++i2]*;
  17. }
  18. if (t == n3) //Watch out, maybe repeated numbers
  19. {
  20. rs[i] = n3;
  21. n3 = rs[++i3]*;
  22. }
  23. if (t == n5) //Watch out, no else!
  24. {
  25. rs[i] = n5;
  26. n5 = rs[++i5]*;
  27. }
  28. }
  29. return rs.back();
  30. }
  31. };

Testing:

  1. int main()
  2. {
  3. unsigned no = getNthUglyNo();
  4. printf("ugly no. is %d \n", no);
  5. vector<int> rs = getAllUglyNo();
  6. for (auto x:rs) cout<<x<<" ";
  7. cout<<endl;
  8.  
  9. UglyNumbers un;
  10. printf("Ugly no. is %d \n", un.getNthUglyNo(, rs));
  11. for (auto x:rs) cout<<x<<" ";
  12. cout<<endl;
  13.  
  14. system("pause");
  15. return ;
  16. }

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. Maven实战——生命周期和插件

    Maven的构建过程包含:初始化.编译.測试.打包.集成測试.部署 Maven拥有三套相互独立的生命周期:clean(清理项目).default(构建项目).site(建立项目网站) 每一个生命周期包 ...

  2. Sort List (使用归并排序的链表排序)

    Sort a linked list in O(n log n) time using constant space complexity. C++代码的实现: #include<iostrea ...

  3. [转] 用source命令执行脚本和用sh执行脚本之间的区别

    from: http://blog.csdn.net/david_xtd/article/details/8012627 问题: 有很多方式可以执行脚本, 1).source test.bsh 2). ...

  4. 深搜最基础题---全排列And组合数

    这个是理解标记和取消标记,用一个vis数组来标记 全排列代码: #include <stdio.h> ]; ]; int n; void dfs(int step)//step是当前已经进 ...

  5. css02基本选择器

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. hdu 2189

    //hdu2189   题意大概就是给n个人,分成多组,要求每组人数都是素数,求有多少种... 解法就是先把150以内的素数全部存入一个数组,然后利用a[j+b[i]]+=a[j];这道题一开始没理解 ...

  7. Java请求参数类QueryParameter

    import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.StringUtils; /** * 请求 ...

  8. Tab标签栏 切换 权威总结

    angular的标签栏,有两种方法实现: 内容全部加载到页面中,再利用ng-show指令. 将每一块要加载的内容做成模板,利用ng-if指令加载. 用bootstrap的tab组件 用angular的 ...

  9. 在Oracle中查询表的大小、表的占用情况和表空间的大小

    转载自http://blog.csdn.net/cuker919/article/details/8514253 select segment_name, bytes as 大小 from user_ ...

  10. C#基础学习第三天(.net菜鸟的成长之路-零基础到精通)

    1.复合赋值运算符 += -= *= /= %= 2.关系运算符  > < >= <= == !=  由关系运算符连接的表达式我们称之为关系表达式.  每一个表达式都可以求解出 ...