Menu Close

Write a program to find the n-th ugly number.

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2,3,5.

Input:  n= 10
Output:  12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note: 1 is typically treated as an ugly number.

n= int(input(''))
list=[]
count=0

i=1
while count<n:
	temp=i
	while(temp%2==0):
		temp=temp/2
	while(temp%3==0):
		temp=temp/3
	while(temp%5==0):
		temp=temp/5
	if temp==1:
		list.append(i)
		count+=1		
	i+=1
	
#print(list)
print(list[n-1])   # list values starts from index value 0

Input_1: Enter the nth value : 7

Output : The last value in the list that is nth ugly value = 8


Input_2: Enter the nth value : 10

Output : The last value in the list that is nth ugly value = 12


Input_3: Enter the nth value : 15

Output : The last value in the list that is nth ugly value = 24


Input_4: Enter the nth value : 1

Output : The last value in the list that is nth ugly value = 1


Input_5: Enter the nth value : 200

Output : The last value in the list that is nth ugly value = 16200


DEMO Output

Executing python code using Linux terminal

More Q