### Ugly Number - Problem 263

Problem:

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.

Calc the prime factors and check whether all factors are in 2, 3, 5.

class Solution(object):
def isUgly(self, num):
"""
:type num: int
:rtype: bool
"""
if num < 1:
return False
if num == 1:
return True
factors = []
i = 2
while i * i <= num:
if num % i == 0:
factors.append(i)
while num % i == 0:
num /= i
i += 1
if num > 1:
factors.append(num)
if len(factors) > 3 or factors[-1] > 5:
return False
return True


Then, it's too heavy... We don't need to calc all the factors.

class Solution(object):
def isUgly(self, num):
"""
:type num: int
:rtype: bool
"""
if num < 1:
return False
for factor in [2, 3, 5]:
while num % factor == 0:
num /= factor
return num == 1


It's clear, yeah. But another...

class Solution(object):
def isUgly(self, num):
"""
:type num: int
:rtype: bool
"""
#n = (2**30)*(3**20)*(5**13) = 4570198050078720000000000000L
return False if num < 1 or (4570198050078720000000000000L) % num != 0 else True


### Ugly Number II - Problem 264

Problem:

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note that 1 is typically treated as an ugly number.

Hint:

The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.

An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.

The Naive Solution(TLE):

class Solution(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
def isUgly(num):
if num < 1:
return False
for factor in [2, 3, 5]:
while num % factor == 0:
num /= factor
return num == 1
count, i = 0, 0
while count != n:
i += 1
if isUgly(i):
count += 1
return i


Spanning the minimum ugly number once a time. Use three queue to store the three ugly number spanning ways. Once choose the minimum number, and move the pointer to the next one.

(1) 1×2, 2×2, 3×2, 4×2, 5×2, …
(2) 1×3, 2×3, 3×3, 4×3, 5×3, …
(3) 1×5, 2×5, 3×5, 4×5, 5×5, …

class Solution(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
ugly = 
c2, c3, c5 = 0, 0, 0
while len(ugly) < n:
u2, u3, u5 = [ugly[c2]*2, ugly[c3]*3, ugly[c5]*5]
m = min(u2, u3, u5)
if m == u2:
c2 += 1
if m == u3:
c3 += 1
if m == u5:
c5 += 1
ugly.append(m)
return ugly[-1]


### Super Ugly Number - Problem 313

Problem:
Write a program to find the nth super ugly number.

Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.

Note:

(1) 1 is a super ugly number for any given primes.

(2) The given numbers in primes are in ascending order.

(3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.

The same way as Ugly Number II does.

class Solution(object):
def nthSuperUglyNumber(self, n, primes):
"""
:type n: int
:type primes: List[int]
:rtype: int
"""
uglys = 
counts =  * len(primes)
while len(uglys) < n:
mlist = [uglys[counts[i]]*primes[i] for i in xrange(0, len(primes))]
m = min(mlist)
uglys.append(m)
for i in xrange(0, len(mlist)):
if mlist[i] == m:
counts[i] += 1
return uglys[-1]


Using Heapq to accelerate the code. Heapq is a module to implement the priority queue. Use heapq to store the temporary spanning minimum ugly number list. Because there is no need to spanning the list in each loop.

class Solution(object):
def nthSuperUglyNumber(self, n, primes):
"""
:type n: int
:type primes: List[int]
:rtype: int
"""
uglys = 
counts =  * len(primes)
mheap = [(uglys[counts[i]]*primes[i], i) for i in xrange(0, len(primes))]
heapq.heapify(mheap)
while len(uglys) < n:
m, i = heapq.heappop(mheap)
counts[i] += 1
if m != uglys[-1]:
uglys.append(m)
heapq.heappush(mheap, (uglys[counts[i]]*primes[i], i))
return uglys[-1]