We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9be99bc commit e633388Copy full SHA for e633388
1 file changed
UGLY NUMBER | LEETCODE
@@ -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
37
38
+# print(isugly(14))
0 commit comments