program periodmax; uses wincrt,wintypes,strings; var r,q,n:array[0..1000] of integer; primo,ultimo,i,j,k,l:integer; a,b:boolean; c:char; BEGIN screensize.x:=80; screensize.y:=300; windowsize.x:=800; windowsize.y:=400; strcopy(windowtitle,'Frazioni con periodo massimale'); repeat clrscr; writeln('Questo programma ricerca le frazioni del tipo 1/n, con n intero'); writeln('e periodo massimale.'); writeln; writeln(' Occorre prestare la massima attenzione ai valori da introdurre da tastiera.'); writeln(' Se non sono del tipo ammesso, il programma si blocca.'); writeln; writeln('Introduci il primo ed ultimo (<=1000) denominatore tra cui eseguire la ricerca.'); writeln; write('Primo denominatore: '); readln(primo); write('Ultimo denominatore: '); readln(ultimo); writeln; k:=primo-1; l:=0; repeat a:=false; i:=2; r[0]:=0; r[1]:=1; k:=k+1; repeat r[i]:=r[i-1]*10 mod k; for j:=0 to i-1 do begin if r[i]=r[j] then a:=true; end; i:=i+1; until (a=true); if ((i-2=k-1) and (r[i-1]=1)) then begin l:=l+1; n[l]:=k; writeln(k:4,' è tale che 1/',k,' ha periodo massimale.'); end until k=ultimo; if l=0 then writeln('Non esistono frazioni con periodo massimale nell''intervallo richiesto.'); if l>0 then begin writeln; writeln('Se vuoi la scrittura decimale di uno di questi numeri,'); writeln('limitata al primo periodo, ''S'', altrimenti ''N''.'); repeat c:=readkey; until ((c='S') or (c='s') or (c='N') or (c='n')); if ((c='S') or (c='s')) then repeat writeln; write('Introduci il denominatore: '); readln(k); a:=false; i:=2; r[0]:=0; r[1]:=1; repeat r[i]:=r[i-1]*10 mod k; q[i]:=r[i-1]*10 div k; for j:=0 to i-1 do begin if r[i]=r[j] then a:=true; end; i:=i+1; until (a=true); b:=false; for j:= 1 to l do begin if n[j]=k then b:=true; end; if b=false then begin writeln; writeln(' Imbroglione!! Il denominatore introdotto non è tra quelli permessi.'); writeln(' In ogni caso, siccome sono paziente, ti do ugualmente la risposta.'); end; writeln; write('La scrittura decimale richiesta è: 1/',k,' = 0,'); for j:=2 to i-1 do write(q[j]); writeln; if l>1 then begin writeln; writeln('Altro denominatore? '); repeat c:=readkey; until ((c='S') or (c='s') or (c='N') or (c='n')); end else c:='n'; until ((c='N') or (c='n')); end; writeln; writeln('Altra ricerca? '); repeat c:=readkey; until ((c='S') or (c='s') or (c='N') or (c='n')); until ((c='N') or (c='n')); donewincrt; END.