473,511 Members | 15,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

algorithm to find nth ugly number

181 Recognized Expert New Member
Can any one give me best algorithm to find nth ugly number.
May 14 '07 #1
10 11432
svlsr2000
181 Recognized Expert New Member
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence

1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...

shows the first 11 ugly numbers. By convention, 1 is included.

the 4th ugliest number is 4, 7th is 8 and so on.
May 14 '07 #2
AdrianH
1,251 Recognized Expert Top Contributor
[font=Times New Roman]Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence [/font]

[font=Times New Roman]1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... [/font]

[font=Times New Roman]shows the first 11 ugly numbers. By convention, 1 is included. [/font]

the 4th ugliest number is 4, 7th is 8 and so on.
First, the tags used here are not a full set of the HTML tags, so font will not work.

Second, we are not here to do your homework for you. If you want help, suggest your algorithm or if you have code you are having problems with, post the relevant code along with the problem you are having. For more information see Posting Guidelines for more information.


Adrian
May 14 '07 #3
svlsr2000
181 Recognized Expert New Member
Second, we are not here to do your homework for you. If you want help, suggest your algorithm or if you have code you are having problems with, post the relevant code along with the problem you are having. For more information see Posting Guidelines for more information.


Adrian
Its not a home work question, i am too old to do homework. :)

I am just reading about some problems and trying to come out with the best solution.

The algorithm i found for this iteratively finds all the n ugly numbers from 1 to n. Hence have higher complexity.
Is there any algorithm which could find nth ugly number without finding the nth ugly number without finding (n-1)th ugly number.
May 14 '07 #4
Ganon11
3,652 Recognized Expert Specialist
Well, you could have a program that would generate X number of ugly numbers into an array. Then, when the user asks for the nth ugly number, you just retrieve array[n-1] and spit that value out.

Ahh, but this still requires you to find the n-1th ugly number...hmmm...
May 14 '07 #5
AdrianH
1,251 Recognized Expert Top Contributor
Its not a home work question, i am too old to do homework.
You are never too old to do homework. I've seen 80 year olds in university classes. :) Who knows, maybe one of them was you. :p ;) :D


Adrian
May 14 '07 #6
AdrianH
1,251 Recognized Expert Top Contributor
Well, you could have a program that would generate X number of ugly numbers into an array. Then, when the user asks for the nth ugly number, you just retrieve array[n-1] and spit that value out.

Ahh, but this still requires you to find the n-1th ugly number...hmmm...
Looks like you are on the right track. Unfortunately, I don't think that there is any O(1) algorithm to calculate it. It would have to be iterative, based on the previous values.

Staring numbers:
2, 3, 5

Next number set:
2 * {2, 3, 5}
+ 3 * {3, 5}
+ 5 * {5}
= { 2*2, 2*3, 2*5, 3*3, 3*5, 5*5 }

...and so on (use combinatorics to get the correct values to multiply).

As you get new numbers, use an appropriate sort algorithm (merge sort is a possibility) to insert them in to the list.

It’s not pretty, but it should work fairly efficiently.


Adrian
May 14 '07 #7
desouky82
1 New Member
could you send me the script if you have it in c
May 21 '07 #8
DeMan
1,806 Top Contributor
By the replies above, it sounds as though the method you are using (which requires you to find all the ugly numbers up to n) is probably about as good as it gets .

The problem with working out the nth without working out the (n-1)th is that at every iteration of the method suggested above, you can introduce new numbers (almost) anywhere, consider:

we know that there are 3 ugly "base" numbers - 2, 3, 5
taking a combination of any 2 of these we know we have a further 6 options (4, 6, 10, 9, 15, 25) (of which 4 is added to the middle of the list - and one is out of order even using this simple algorithm) giving 2,3,4,5,6,9,10,15,25). The next iteration, gives 8, 12, 20, 18, 30, 50, 30, 27, 45, 75, 50, 125 {i think} and it starts to become clear that we can be inserting elements almost anywhere. There is a lower limit but you can only know this lower limit by showing that each number before either is definitely or couldn't possibly be an ugly number.

Thus you cannot say that x is the nth ugly number without knowing what the (n-1)th is
May 21 '07 #9
lalsheyal
1 New Member
Declare an array for ugly numbers.
Initialize the first ugly number to be 1 and 3 array index variables to point to the first element of the ugly number array.
Fill up the ugly number array : The next ugly number will be the multiplication of the existing ugly numbers with 2, 3 or 5. The existing ugly numbers are multiplied by 2, 3 and 5. In each cases choose the product of the multiplication that is the next greater integer number of the
already found biggest ugly number. The next ugly number will be the minimum of these three numbers.
Repeat the above until the desired indexed ugly number is found.

For code http://www.algoqueue.com/algoqueue/d...th-ugly-number
Jun 21 '14 #10
manageknowledge
10 New Member
Use recursion,
if you know n-1th ugly number, find out the composition
and then increase one composition at a time and whichever is nearest would be nth ugly number.
Jun 22 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

16
2637
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
1
2391
by: tuko | last post by:
Hello kind people. I have the classes "point", "curve", "surface", "region" as shown below. Every class has a member function that returns a list of pointers to objects that comprise the *this...
32
76373
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
2
2091
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
113
12186
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
6
25349
by: aarklon | last post by:
Hi folks, I found an algorithm for calculating the day of the week here:- http://www.faqs.org/faqs/calendars/faq/part1/index.html in the section titled 2.5 what day of the week was 2 august...
4
4175
by: mkppk | last post by:
MICR = The line of digits printed using magnetic ink at the bottom of a check. Does anyone know of a Python function that has been written to parse a line of MICR data? Or, some financial...
4
32047
prometheuzz
by: prometheuzz | last post by:
Hello (Java) enthusiasts, In this article I’d like to tell you a little bit about graphs and how you can search a graph using the BFS (breadth first search) algorithm. I’ll address, and...
10
13617
by: socondc22 | last post by:
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.