pascal求数的质因子

来源:百度知道 编辑:UC知道 时间:2024/06/22 09:03:50
求2-100中,每个数的质因子,输出如下形式:
2=2
3=3
4=2*2
……
100=2*2*5*5
在找质因子的过程中,可以不必判断它是否为素数,只要算法合适,所求的因子必然是质因子

我的程序比楼上的更清晰易懂,因为质数除了2以外,其他都是奇数,所以我的程序运行速度也比楼上的程序要快一倍。
program zhiyinzi_Hewr;

const
maxn=100;

var
n:longint;

procedure print(x:longint);
var
i:longint;
first:boolean;
begin
write(x,'=');
first:=true;
i:=1;
while (n>1) and (n mod 2=0) do begin
if not first then write('*');
write(2);
first:=false;
n:=n div 2;
end;
while n>1 do begin
inc(i,2);
while n mod i=0 do begin
if not first then write('*');
first:=false;
write(i);
n:=n div i;
end;
end;
end.

begin
for n:=2 to maxn do print(n);
end.

var
a:array[1..100]of integer;
i,j,n,k:integer;
begin
a[1]:=2;n:=1;
for i:=3 to 100 do
for j:=1 to n do
if (a[j]<trunc(sqrt(i))) then
if i mod a[j]=0 then break else
else
begin