Title: We included only factors 2, 3 and number 5 is referred to as ugly numbers. Demand from small to large number of 1500 ugly sequence of practice, we put 1 as the first ugly number.
def ugly_num(n): if n<1: return 0 res = [0,1,2,3,4,5] i = 6 t2, t3, t5 = 1, 1, 1 while i<=n: while res[t2]*2<=res[-1]: t2 += 1 while res[t3]*3<=res[-1]: t3 += 1 while res[t5]*5<=res[-1]: t5 += 1 num = min([res[t2]*2,res[t3]*3,res[t5]*5]) res.append(num) i += 1 return res[n]
NOTE: In order to utilize several ugly previously computed using a number of ugly list storage to be calculated. Ugly before using number 3 pointers represent 2,3,5 multiplied just greater than the current position of the last list number ugly. Ugly new number to be added is the number of ugly 3 are multiplied by the minimum value of the pointer after 2,3,5, 2,3,5 then update the pointer position.