Ugly numbers

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

class Solution(object):
    def isUgly(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num<1:
            return False

        ugly = [2,3,5]
        for i in ugly:
            while num % i==0:
                num /= i          

        return num==1
时间: 2024-12-29 15:35:07

Ugly numbers的相关文章

poj 1338 Ugly Numbers

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21315   Accepted: 9520 Description 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, ... shows the first 10 ugly num

NYOJ1097 Ugly Numbers 【丑数】

Ugly Numbers 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 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. Now give you need

Ugly Numbers(1.5.8)

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64 Description 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, ... shows the first 10 ugly numbers. By convention,

poj1338 Ugly Numbers(丑数模拟)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1338 Description 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, ... shows the first 10 ugly number

丑数(Ugly Numbers,UVA 136)

#include<iostream> #include<set> #include<vector> #include<queue> using namespace std; const int coeff[3]={2,3,5}; typedef long long LL; int main() { priority_queue<LL,vector<LL>,greater<LL> > pq; set<LL> s;

UVa 136 Ugly Numbers(优先队列)

题意  质因数只可能有2,3,5的数称为丑数  输出第1500个丑数 STL优队列应用  1是丑数 丑数的2,3,5倍都是丑数  用优先队列模拟就行了 #include <cstdio> #include <cstring> #include <set> #include <queue> using namespace std; typedef long long ll; //priority_queue<ll, vector<ll>, g

UVA 136 Ugly Numbers

原题代号:UVA 136 原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=72 题目原题: Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5,

UVA 136 &amp; POJ1338 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, - shows the first 10 ugly numbers. By convention, 1 is included. 把只含有2.3.5因数的数称为丑数,默认第一个丑数是1. POJ是有多次询问,输出第n个丑数 UVA是询问第1500个丑数是多少. 思路:

POJ1338 Ugly Numbers(解法二)

问题链接:POJ1338 Ugly Numbers.基础级练习题,用C语言编写程序. 题意简述:不能被2.3和5以外的素数整除的数称为丑数,找出第1500个丑数. 问题分析:换句话说,丑数的因子只能是2.3和5.1是丑数,对于x,若x是丑数则2x.3x和5x是丑数.利用已知的丑数,从小到不断生成丑数就可以了. 程序中,结果放在数组ans[]中,也是生产丑数的x:素数放在数组prime[]中,这个问题只有2.3和5:生成的丑数放在数组ugly[]中,然后选出最小的放入结果数组ans[]中,对于被放