# 丑数相关问题

• 可以尝试用空间换取时间
• 像丑数这种越来越稀疏的问题，越到后面越体现出算法优化的重要性

《剑指offer34题》只包含因子2、3、5的数叫丑数，找出前1500个丑数。
1.暴力解法

``````
def is_ugly_num(num):
while num % 2 == 0:
num //= 2

while num % 3 == 0:
num //= 3

while num % 5 == 0:
num //= 5

if num == 1:
return True
else:
return False

@timing
def get_ugly_num2(index = 1500):

i = 0
num = 2
while i < index:
while not is_ugly_num(num):
num += 1
# print(num)
i += 1
num += 1
``````

500个之前的差别：
get_ugly_num function took 0.002 s
get_ugly_num2 function took 3.665 s

2.正向求解的方法，秒解，暴力破解几分钟还是没有完，我等不及了

``````@timing
def get_ugly_num(index = 1500):
if index < 1:
return

ugly_list = [1]
current_ugly_index = 0
index_2 = 0
index_3 = 0
index_5 = 0
num_2 = 1
num_3 = 1
num_5 = 1
while current_ugly_index < index :

while num_2 <= ugly_list[current_ugly_index]:
index_2 += 1
num_2 *= 2

while num_3 <= ugly_list[current_ugly_index]:
index_3 += 1
num_3 *= 3

while num_5 <= ugly_list[current_ugly_index]:
index_5 += 1
num_5 *= 5

min_num = min((num_2,num_3,num_5))
ugly_list.append(min_num)
current_ugly_index += 1
# print(min_num)
``````