{Jozef Sivek ml. januar 2005
program na prvociselny rozklad cisel
}

program p_rozklad;

const hh_r=100;

type rozklad=array[1..hh_r,1..2] of longint;

var tab:rozklad;
    input,my_input:longint;
    prvo:longint;
    position:byte;
    last_d:longint;
    out:boolean;
    I:integer;

procedure clean_tab;
          var I:byte;

          begin
          for I:=1 to hh_r do begin
                              tab[I,1]:=0;
                              tab[I,2]:=0;
                              end;
          end;

function prvocislo(input:longint):longint;
         var hh,I:longint;

         begin

         input:=abs(input);

         hh:= trunc (sqrt(input) + 1);

         case input of
         0,1:prvocislo:=-1;

         2: prvocislo:=2;

         else begin
               for I:=2 to hh do begin

                                 if (I<input) and ((input mod I) = 0) then begin
                                                          prvocislo:=I;
                                                          exit;
                                                                           end;
                                 end;

               prvocislo:=input;
              end;

         end;

         end;

begin
writeln('Prvociselny rozklad, od J.S.2005');
writeln('pre ukoncenie staci namiesto cisla:longint zadat nulu');

repeat

 writeln('____________________________________________________________');

 repeat
 writeln;write('cislo? = ');readln(my_input);
 input:=my_input;
 if input=0 then halt;
 if input<0 then writeln('error: only natural numbers! If you want to exit enter 0');
 if input=1 then writeln('no comment');
 until input>1;

 clean_tab;
 position:=1;
 last_d:=0;

 repeat

  prvo:=prvocislo(input);

  if prvo>=0 then begin
                  case last_d of
                  0: begin
                     tab[position,1]:=prvo;
                     inc(tab[position,2]);
                     last_d:=prvo;
                     input:=input div prvo;
                     end;

                  else begin

                              if last_d=prvo then begin
                                             inc(tab[position,2]);
                                             input:=input div prvo;
                                                  end;
                              if last_d<>prvo then begin
                                              inc(position);
                                              tab[position,1]:=prvo;
                                              inc(tab[position,2]);
                                              last_d:=prvo;
                                              input:=input div prvo;
                                                  end;
                       end;

                  end;
                  end;

 until prvo<=0;

 out:=false;
 position:=1;
 write(my_input:10,' = ');

 repeat
 if tab[position,1]<>0 then if tab[position,2]>1 then write(tab[position,1]:5,'^(',tab[position,2]:1,')')
                            else write(tab[position,1]:5)
    else out:=true;

 inc(position);
 if tab[position,1]<>0 then write(' * ');

 until out;
 writeln;


until input=0;

end.
