To get the prime factors of a number you only need to divide it with the primes which r less than or equal to its square root. For example to get the prime factors of 69, you only need to divide it with 2, 3, 5, 7 and then the number that remains is 23 which is greater than its square root and is always prime or 1. So you need to generate primes upto sqrt(Max input).
My solution should give you compile error. I don't know why but there
may be some error when I was editing it in the poll editor.
See,I have corrected it. (There was a declaration double a;it should be
Send it, And see,It will get accepted ,no Runtime Error.
And I cannot find any valid input for domain error.If the input is 2^31
or -2^31 it gives a wrong output but one can easily overcome it.
All judge inputs are [-2^31+1 , 2^31-1]
[pascal] var code,c:integer; j,i,n:longint; s:string;
function prime(n:longint):boolean; begin prime:=true; for j:=2 to trunc(sqrt(n)) do if n mod j=0 then begin prime:=false; exit; end; if c=1 then write(' x '); write(n); end;
procedure rec(n:longint); begin inc(i); while n mod i=0 do begin n:=n div i; if c=0 then begin write(i); c:=1; end else write(' x ',i); end; if prime(n) then n:=1; if n<>1 then rec(n); end;
begin readln(s); while s<>'0' do begin c:=0; write(s,' = '); if s='-2147483648' then begin s:='2147483648'; c:=1; write(' x ',-1); end; if s='2147483648' then begin s:='1073741824'; if c=0 then write(2) else write(' x ',2); c:=1; end; val(s,n,code); if n<0 then begin n:=abs(n); write(-1); c:=1; end; i:=1; if n<>1 then rec(n) else if s<>'-1' then write(1); writeln; readln(s); end; end.[/pascal]