Skip to content

Commit e633388

Browse files
authored
UGLY NUMBER | LEETCODE
Initial File
1 parent 9be99bc commit e633388

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

UGLY NUMBER | LEETCODE

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#DYNAMIC APPROACH
2+
#Find Out nthUgly Number
3+
4+
def nthUglyNumber(n):
5+
res = [1]
6+
i2, i3, i5 = 0, 0, 0
7+
for i in range(n - 1):
8+
next2 = res[i2] * 2
9+
next3 = res[i3] * 3
10+
next5 = res[i5] * 5
11+
next = min(next2, next3, next5)
12+
res.append(next)
13+
if next == next2:
14+
i2 += 1
15+
if next == next3:
16+
i3 += 1
17+
if next == next5:
18+
i5 += 1
19+
return res[-1]
20+
21+
print(nthUglyNumber(9))
22+
23+
24+
#Check whether number is ugly or not
25+
26+
def isugly(num):
27+
if num==0:
28+
return False
29+
factor=[2,3,5]
30+
for f in factor:
31+
while (num%f==0):
32+
num=num/f
33+
if num==1:
34+
return True
35+
else:
36+
return False
37+
38+
# print(isugly(14))

0 commit comments

Comments
 (0)