Phi (Member)
Expert
Messaggi: 241
Iscritto: 30/12/2009
|
Per una "sfida" con un amico ho cercato di fare un programma per calcolare un numero primo di 100 cifre.
program primo100;
type
cifra = type $00..$09;
numero = array[1..100]of byte;
var
cont : word;
procedure dim(var a:numero; const c : byte);
begin
if a[c]=0 then begin
dim(a,c+1);
a[c]:=9;
end else a[c] := a[c]-1;
end;
procedure aum(var a:numero; const c : byte);
begin
if a[c]=9 then begin
aum(a,c+1);
a[c]:=0;
end else a[c] := a[c]+1;
end;
operator -(a, b : numero) r : numero;
begin
for cont := 1 to 100 do
if a[cont]>=b[cont]then r[cont] := a[cont]-b[cont]
else begin
dim(a,cont+1);
r[cont] := a[cont]+10-b[cont];
end;
end;
operator =(a, b : numero) r : boolean;
begin
r := true;
for cont := 1 to 100 do if a[cont]<>b[cont] then begin
r := false;
exit;
end;
end;
operator >(a,b : numero) r : boolean;
begin
if a=b then r := false;
for cont := 100 downto 1 do begin
if a[cont]>b[cont] then begin r := true; exit; end;
if a[cont]<b[cont] then begin r := false; exit; end;
end;
end;
operator >=(a,b : numero) r : boolean;
begin
r := (a=b) or (a>b);
end;
procedure scrivi(n : numero);
begin
for cont := 100 downto 1 do write(n[cont]);
writeln;
end;
function euclide(a, b : numero) : numero;
begin
while a<>b do begin
if a=b then begin euclide := a; exit; end;
if a>b then a := a-b
else b := b-a;
end;
euclide := a;
end;
var
N, divis : numero;
primo : boolean;
const
uno : numero = (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
inizio : numero = (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1);
fine : numero = (1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2);
radice : numero = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,9,0,2,4,2,7,8,8,6,1,0,8,8,4,0,5,9,0,3,7,3,2,6,5,3,1,2,4,1,4,1);
BEGIN
readln;
n := inizio;
repeat
primo := true;
divis:=uno;
repeat
aum(divis,1);
aum(divis,1);
if euclide(n,divis)<>uno then primo := true;
until (divis=radice)or not(primo);
writeln;
scrivi(n);
if primo then begin
writeln('!! E' primo !!');
readln;
end else writeln('non è primo');
aum(n,1);
aum(n,1);
until N = fine;
readln;
END.
Però si blocca e va avanti all'infinito quando si tratta di calcolare il M.C.D
(se quancuno non l'avesse capito questo è compito delle funzione Euclide, che sfrutta, appunto l' algoritmo ideato da Euclide: quello di ab-cd=(a-c)b
|