Page 4 of 11
Posted: Wed Apr 23, 2003 12:40 pm
is it trully 42 ?

how can it be ? isn't it should be thousands may be greater .. ?

Posted: Wed Apr 23, 2003 1:37 pm
Yes~

Posted: Wed Apr 23, 2003 1:37 pm
Nah, I just couldn't resist.

You don't seriously think anyone would give you the answer?
Then you could just print it and there would be no more fun.

Let me put it this way: if you count up by one every second, it would take you some 27 years and 3 months to reach the answer. Considerably less than the 7,500,000 years it took Deep Thought to come up with the answer 42!

Posted: Sat Apr 26, 2003 4:39 pm
little joey wrote:You don't seriously think anyone would give you the answer?
Then you could just print it and there would be no more fun.
If somebody really want to get accepted that way, it's really easy to write a brute force algorithm just to get the answer and then print it. But who can be proud of such a code.
Anyway, the solution is probably already written in others topics. Even the source is somewhere else. Everyone can send it. He/She will get one more problem accepted (very impressive ) and can only get for this problem 1900th with 0:00.000/64 .

### O(n^2)

Posted: Thu May 01, 2003 7:17 am
Especially when a very simple O(n^2) algorithm exists, as well as some more complex ones that run faster. But we're taking sub .1 second times here.

Posted: Thu May 08, 2003 8:28 am
859963392
But just for test, not for AC purpose!!!!![/b]

Posted: Thu May 08, 2003 2:18 pm
hello...

I think for test your algo correct or not, you can try to solve problem "humble number " http://acm.uva.es/p/v4/443.html both of them can be solved with same algorithm.

Posted: Fri May 09, 2003 8:09 am
a nice variant of this problem would be to make the number of prime factors variable.
the input would look like:

2
2 3
100

4
3 11 31 37
222

etc...

i'm sure it would heat some brains.

### 136 WA

Posted: Thu Jul 03, 2003 7:13 am
What is wrong with my code ?
can anybody tell me ? I think I've missunderstood the problem..

[c]/* UGLY NUMBER */

#include <stdio.h>

#define true 1
#define false 0
#define boolean unsigned int

boolean IsPrime (int x);
/* Determine if x is a prime number or not*/

boolean Is235 (int x);
boolean IsExistOther (int x);
int main()
{
int ctr, i, j,bil;

ctr = 0;
bil = 1;

clrscr();
while (ctr < 1500)
{
if ((Is235(bil)) || (bil == 1))
{
if (bil == 1)
{

ctr++;
bil++;
}
else if (IsExistOther (bil))
{
bil++;
}
else
{

ctr++;
bil++;
}
}
else
{
bil++;
}
}

printf("The 1500'th ugly number is : %d", bil );

return 0;
}

boolean IsPrime (int x)
{
int cprime, i;

if (x == 1)
{
return true;
}
else
{
cprime = 0;
for (i=1; i<= x ; i++ )
{
if (x % i == 0)
{
cprime++;
}
}
if (cprime == 2)
{
return true;
}
else
{
return false;
}
}
}

boolean Is235 (int x)
{
return ((x % 2 == 0) || (x % 3 == 0) || (x % 5 == 0));
}

boolean IsExistOther (int x)
{
int i, error;

error = 0;
for (i = 1; i<= x ;i++ )
{
if ( x % i == 0)
{
if ((IsPrime(i)) && ((i!= 2) || (i!= 3) || (i != 4)))
{
error++;
}
}
}
if (error == 0)
{
return true;
}
else
{
return false;
}
}[/c]

Posted: Thu Jul 03, 2003 3:35 pm
i didnt check completely your code, since i dont have compiler here. but there are several things that you should notice..
1. do not need to use clrscr
2. check your output and compare it with the sample output in the problem description. there are two mistakes.
3. i think you misunderstood about prime factors, or perharps you write wrong code. and also notice that its only prime factors is 2, 3 and 5.
hope it can help and good luck.

titid

### 136 in Scheme :-)

Posted: Mon Jul 28, 2003 8:44 pm
Hi!

With a task like that one I really regret we can't submit solutions in Lisp/Scheme. Look at the code: isn't it just beautiful? Of course, I looked up the right answer with it and made a valid c++ program, but it's not the same
There is the Bigloo, which can be used as a converter from Scheme to C, but unfortunalety I don't think it is available during ACM contests

Code: Select all

``````(define-macro (cons-stream  head tail) `(cons (delay ,head) (delay ,tail)))
(define (stream-car s) (force (car s)))
(define (stream-cdr s) (force (cdr s)))

(define empty-stream '())
(define stream-null? null?)

(define (const-stream c) (cons-stream c (const-stream c)))

(define (stream-ref s n)
(if (= n 0)
(stream-car s)
(stream-ref (stream-cdr s) (- n 1))))

(define (stream-map s f)
(if (stream-null? s)
empty-stream
(cons-stream (f (stream-car s)) (stream-map (stream-cdr s) f))))

(define (scale-stream s c)
(stream-map s (lambda(x) (* x c))))

(define (stream-merge s1 s2)
(cond ((stream-null? s1) s2)
((stream-null? s2) s1)
(else
(let ((x1 (stream-car s1))
(x2 (stream-car s2)))
(cond ((< x1 x2) (cons-stream x1 (stream-merge (stream-cdr s1) s2)))
((< x2 x1) (cons-stream x2 (stream-merge s1 (stream-cdr s2))))
(else (cons-stream x1 (stream-merge (stream-cdr s1) (stream-cdr s2)))))))))

(define s (cons-stream 1 (stream-merge (stream-merge (scale-stream s 2) (scale-stream s 3)) (scale-stream s 5))))

(stream-ref s 1499)``````

Posted: Tue Jul 29, 2003 9:12 am
This is a simple problem. you have to make output only one thing. So you use just make a precalculation and then just make the output.

so Simpleeeeeeeee........

Posted: Tue Jul 29, 2003 9:29 am
just use precalculation and then print te rsult

my precalculation program tooks 42 min of time to get the result

i got AC

Posted: Tue Jul 29, 2003 1:19 pm
And what if I tell you that the code runs for 0.1 s to get the right answer and it could easily count, lets say, 100000-th ugly number in 10s. time limit?

Actually, as I said before, this is my precalcutaion program. Does anyone have faster one?

And, by the way, could you count the 300000-th ugly number in 15s.? If you do, we can compare our results, mine is here:

62864273786544799804687500000000000000000000000000000000

All I did to get it was to change 14999 into 299999 in my code. Nothing else was necessary.

Posted: Tue Jul 29, 2003 1:21 pm
Oh, now I see that your code was running for 42 min. to solve the problem for 1500-th ugly number. Can you see the difference?