Leetcode Ugly Number
2015-09-08 08:53
344 查看
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include
Note that
最直接的办法就是不停的除以这些质数,如果剩余的数字是1的话就是Ugly number了
Java code:
Reference:
http://www.cnblogs.com/grandyang/p/4741934.html
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5. For example,
6, 8are ugly while
14is not ugly since it includes another prime factor
7.
Note that
1is typically treated as an ugly number.
最直接的办法就是不停的除以这些质数,如果剩余的数字是1的话就是Ugly number了
Java code:
public boolean isUgly(int num) { if(num <=0) { return false; } while(num %2 == 0) { num /= 2; } while(num %3 == 0) { num /= 3; } while(num %5 == 0) { num /= 5; } return num == 1; }
Reference:
http://www.cnblogs.com/grandyang/p/4741934.html
相关文章推荐
- Fresco 源码分析(二) Fresco客户端与服务端交互(3) 前后台打通
- synchronized详解
- markdown在博客园的使用
- SQL 2000 开启远程
- strcat 函数的使用指南
- 信大捷安第一届Dota比赛
- gen already exists but is not a source folder. Convert to a source folder or rename it.
- CLR via C#
- 递归练习---根据要求列出文件夹的子孙目录
- ADO,ADO.NET
- 递归:函数的自身调用函数的自身。
- 转换流
- leetcode 278: First Bad Version
- 像专家一样思考,像专家一样实践
- UI:UINavigationController、界面通信
- noip2005 谁拿了最多奖学金 (模拟)
- 学习矢量量化(LVQ)
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- C++条件语句和条件运算符的使用方法讲解
- 编码与解码