丑数

题目描述

  编写一个程序,找出第 n 个丑数,并给出该程序的时间复杂度。丑数:质因数只包含2,3,5的正整数。n<1690。质因数:能整除给定正整数的质数。

def fun(num):
  if num<1:
    return 0
  temp = 0
  a2 = a3 = a5 =0
  while len(temp)<num:
    temp.append(min(temp[a2]*2,temp[a3]*3,temp[a5]*5))
    while temp[a2]*2<=temp[-1]:
      a2+=1
    while temp[a3]*3<=temp[-1]:
      a3+=1
    while temp[a5]*5<=temp[-1]:
      a5+=1
  return temp[-1]

提示:

  step1: temp = [1] , t2 = 0 , t3 = 0 , t5 = 0

  step2: temp = [1,2] , t2 = 1 , t3 = 0 , t5 = 0

  step3: temp = [1,2,3] , t2 = 1 , t3 = 1 , t5 = 0

  step4: temp = [1,2,3,4] , t2 = 2 , t3 = 1 , t5 = 0

  step5: temp = [1,2,3,4,5] , t2 = 2 , t3 = 1 , t5 = 1

  ................................  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/hanouba/p/13950845.html