-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path263 Ugly Number.py
40 lines (30 loc) · 905 Bytes
/
263 Ugly Number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
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.
Author: Rajeev Ranjan
"""
class Solution(object):
def isUgly(self, num):
"""
Prime factors: 2, 3, 5
:type num: int
:rtype: bool
"""
if num < 1:
return False
if num == 1:
return True
ugly = {2, 3, 5}
prime = 2
while prime*prime <= num and num > 1:
if num % prime != 0:
prime += 1
else:
num /= prime
if prime not in ugly:
return False
if num not in ugly:
return False
return True