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, and n does not exceed 1690.
1 class Solution: 2 3 def nthUglyNumber(self, n): 4 """ 5 :type n: int 6 :rtype: int 7 """ 8 l= [] 9 l.append(1) 10 t2,t3,t5=0,0,0 11 mins = 0 12 while len(l)<n: 13 mins = min(l[t2]*2,l[t3]*3,l[t5]*5) 14 l.append(mins) 15 if(mins == l[t2]*2): 16 t2+=1 17 if(mins == l[t3]*3): 18 t3+=1 19 if(mins == l[t5]*5): 20 t5+=1 21 return l[-1]