Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Answers
Answer:
Explanation:
# 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(int(input('Enter the value of n:')))
print("150th ugly no. is ", no)
Explanation:
Answer:
Explanation:
# 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(int(input('Enter the value of n:')))
print("150th ugly no. is ", no)