UVa 583 - Prime Factors

583 - Prime Factors

 * http://acm.uva.es/p/v5/583.html

Summary
This problem seems to have an extended timelimit (30 seconds rather than 10 seconds), which makes a near Bruteforce solution even more feasible.

Explanation
While a completely dumb approach to factoring a number $$n$$ is to try dividing by all possible factors $$f$$ in the range $$2 \le f \le n - 1$$ and checking their remainder, this will timeout. However, a slightly smarter approaching, trying all factors f in the range $$2 \le f \le \sqrt{n}$$ will run in time.

The reason checking the reduced set of candidates works is essentially that the smaller factor is never bigger than $$\sqrt{n}$$, a very short proof follows.

Consider a positive integer $$n$$ with positive factors $$a$$ and $$b$$, so $$n = a \times b$$. Without loss of generality, choose to label the smaller number $$a$$. So then $$a \le b$$, and $$a \times a \le a \times b \le n$$, and therefore $$a \le \sqrt{n}$$.

Gotchas
Negative numbers are allowed, negate the input and remember the $$-1$$ multiple before factoring.

Optimizations
These are not neccesary but will give you a faster time. The first optimization is simple, 2 is the only even prime, so after two, we can check only odd factors, which will reduce the possible factor space by a factor of 2, and give a nearly equal speed up.

An optimization that is a bit harder to implement is to pre-compute all the primes $$p$$ in the range $$2 \le p \le \sqrt{2^{31}}$$ with the Prime Sieve of Eratosthenes and use that as the candidate list.

Input
Input Here

Output
Output Here