40 lines
853 B
Text
40 lines
853 B
Text
\ Prime decomposition
|
|
code Abs=0, Rem=2, CrLf=9, IntIn=10, IntOut=11, Text=12;
|
|
define MaxFacIndex = 30;
|
|
\ -(2^31) has most prime factors (31 twos) than other 32-bit signed integer.
|
|
integer I, N, Facs(MaxFacIndex), FacsCnt;
|
|
|
|
procedure CalcFacs(N, Facs, FacsCnt);
|
|
integer N, Facs, FacsCnt;
|
|
integer I, Cnt;
|
|
begin
|
|
N:= Abs(N);
|
|
Cnt:=0;
|
|
if N >= 2 then
|
|
begin
|
|
I:= 2;
|
|
while I * I <= N do
|
|
begin
|
|
if Rem(N / I) = 0 then
|
|
begin
|
|
N:= N / I;
|
|
Facs(Cnt):= I; Cnt:= Cnt + 1;
|
|
I:= 2
|
|
end
|
|
else
|
|
I:= I + 1
|
|
end;
|
|
Facs(Cnt):= N; Cnt:= Cnt + 1
|
|
end;
|
|
FacsCnt(0):= Cnt
|
|
end;
|
|
|
|
begin
|
|
Text(0, "Enter a number: "); N:= IntIn(0);
|
|
CalcFacs(N, Facs, addr FacsCnt);
|
|
for I:= 0 to FacsCnt - 2 do
|
|
begin
|
|
IntOut(0, Facs(I)); Text(0, " ")
|
|
end;
|
|
IntOut(0, Facs(FacsCnt - 1)); CrLf(0)
|
|
end
|