def ugly(pos): count = 0 uglys = [2,3,5] minVal = 0 while count != pos-1: minVal = min(uglys) if uglys.count(minVal*2)==0: uglys.append(minVal*2) if uglys.count(minVal*3)==0: uglys.append(minVal*3) if uglys.count(minVal*5)==0: uglys.append(minVal*5) uglys.remove(minVal) count += 1 print minVal if __name__=='__main__': ugly(1500)
def ugly(pos) count = 0 uglys = [2,3,5] minVal = 0 while count != pos-1: minVal = uglys.min if uglys.index(minVal*2)==nil: uglys.insert(-1,minVal*2) end if uglys.index(minVal*3)==nil: uglys.insert(-1,minVal*3) end if uglys.index(minVal*5)==nil: uglys.insert(-1,minVal*5) end uglys.delete(minVal) count += 1 end puts minVal end ugly(1500)
log2 = 0.3010 log3 = 0.4771 log5 = 0.6987 2 = log2 3 = log3 5 = log5 2*2 = 2^2 = (log2)^2 = 2*log2 2*3 = log(2*3) = log2 + log3 2*2*3 = log(2*2*3) = log2 + log2 + log3