一个正整数的因数只有1、2、3、5和它本身,那么这个数就是丑数。那么丑数从小到大的序列为 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … . 规定,1为最小的丑数。
问题:给出一个正整数n,找到第n个丑数。
方法一:
遍历所有正整数,判断其是否为丑数;
当丑数计数器count等于n时停止。
# Python3 code to find nth ugly number
# This function divides a by greatest
# divisible power of b
def maxDivide(a,b):
while a % b ==0:
a = a /b
return a
# Function to check if a number
# is ugly or not
def isUgly( no ):
no = maxDivide(no,5)
no = maxDivide(no,3)
no = maxDivide(no,2)
if no