forked from Aspirer23/Dynamic-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuglyno.py
More file actions
35 lines (29 loc) · 759 Bytes
/
uglyno.py
File metadata and controls
35 lines (29 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Python3 code to find nth ugly number
# This function divides a by greatest
# divisible power of b
def maxDivide( a, b ):
while a % b == 0:
a = a / b
return a
# Function to check if a number
# is ugly or not
def isUgly( no ):
no = maxDivide(no, 2)
no = maxDivide(no, 3)
no = maxDivide(no, 5)
return 1 if no == 1 else 0
# Function to get the nth ugly number
def getNthUglyNo( n ):
i = 1
count = 1 # ugly number count
# Check for all integers untill
# ugly count becomes n
while n > count:
i += 1
if isUgly(i):
count += 1
return i
# Driver code to test above functions
no = getNthUglyNo(150)
print("150th ugly no. is ", no)
# This code is contributed by "Sharad_Bhardwaj".