Click to See Complete Forum and Search --> : Coding problem. Please HELP!!!!!!!!!!


UI-ZEIKVK
08-09-2008, 12:17 AM
Can anyone help on coding this problem???

Question:
Ugly numbers are 1 , 2 , 3 , 5 so what is the 1500 th Ugly Number?


I'm Java novice. Please help?

scragar
08-09-2008, 12:24 AM
...?

I'm intrigued, but I have no idea what your pattern is, could it be the Fibonacci sequence (http://en.wikipedia.org/wiki/Fibonacci_number) or a list of primes, no clues coming?

UI-ZEIKVK
08-09-2008, 12:31 AM
Fist of all thanks for the Quick Reply scrager!

Ugly numbers are numbers whose only prime factors are 2, 3 or 5.

i missed it, Sorry for that

scragar
08-09-2008, 12:42 AM
Fist of all thanks for the Quick Reply scrager!

Ugly numbers are numbers whose only prime factors are 2, 3 or 5.

i missed it, Sorry for that
that's Hamming's problem, well known problem to teach the basics of algorithms, not a tricky task at all.
I'll write the method, you write the code(after a search for ugly numbers turned up a page that asks you to find these very same ugly numbers in java, sounds like homework or some such. I'm not doing your homework).

EDIT: oops, wrong solution(that's a different problem, I should have looked back at the problem first, it is hammings problem though)

UI-ZEIKVK
08-09-2008, 12:50 AM
This is not homework. Just i need to find the solution. The way. Yes it's a well known problem. But i need to know how i do this with java. I'm java beginner.

Once again thanks for the reply.

scragar
08-09-2008, 12:59 AM
http://www.ececs.uc.edu/~franco/C321/html/hamming.java.html

just google it if you want the answer, what's more important is not the answer, but the way of finding the answer.

UI-ZEIKVK
08-09-2008, 01:16 AM
Thanks! Yes, the important thing is the way.

scragar
08-09-2008, 01:36 AM
the idea is pretty simple, but can be rather confusing, the best way is to build a multiplication matrix like so(this is not java, this is mathematical notation.)
{2, 3, 5} * {2, 3, 5} = { 4, 6, 10
6, 9, 15
10, 15, 25}
removing repeats you get: {4, 5, 9, 10, 15, 25} which is added to the list:
{1, 2, 3, 4, 5, 6, 9, 10, 15, 25}
you then repeat this process on the newly added numbers(provided they were not in the list to begin with)
{4, 5, 9, 10, 15, 25} * {2, 3, 5} = { 8, 10, 18, 20, 30, 50
12, 15, 27, 30, 45, 75
20, 25, 45, 50, 75,125}

again, removing duplicates:
{8, 1218, 20, 25, 27, 30, 45, 50, 75, 125}

this is repeated, ignoring any number over the match point until your smallest term the loop throws up is over your match point(and thus nothing goes back into the loop. for you the match point is 2^12 * 3^12 * 5^12 - these numbers get big, fast, so don't be scared by that things size if you work it out - since 12^3 ~= 1700)
at which point answer 1499 in the array is the one you want(since the array won't store 1). easy enough?

UI-ZEIKVK
08-09-2008, 01:49 AM
OK. And now is the hard time begins. Thanks scrager. i'll go through your instructions.