Tin học Chương trình viết ra tất cả số nguyên tố theo yêu cầu

dangxuanchuon

Học sinh
Thành viên
31 Tháng bảy 2022
110
61
46
Phú Yên
[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

Chương trình này sử dụng "công nghệ" sàng Eratosthenes để tạo ra danh sách:
Mã:
program sang;
uses crt;
const max = nhap so vao;
var o: array [2..max] of boolean;
i, j, k: uint64;
t: text;
//ts: array [1..max div s] of text;
//f: byte;
{function st(_y:byte) : char;
        begin
        if _y=0 then st:='0';
        if _y=1 then st:='1';
        if _y=2 then st:='2';
        if _y=3 then st:='3';
        if _y=4 then st:='4';
        if _y=5 then st:='5';
        if _y=6 then st:='6';
        if _y=7 then st:='7';
        if _y=8 then st:='8';
        if _y=9 then st:='9';
        end;
function str(_x: uint64): string;
var _i: byte;
_xx: uint64;
        begin
        _xx:=_x;
        repeat
                str:=str+st(_xx mod 10);
                _xx:= _xx div 10;
        until _xx<10;
        end;}
begin
clrscr;
textcolor(red);
Writeln('Tao cac so nguyen to duoi ', max, '.');
textcolor(white);
gotoxy(5,3);
write('Nhan enter de tiep tuc.');
readln;
gotoxy(1,3);
Delline;
textcolor(green);
gotoxy(5,4);
writeln('Dang tinh toan:');
//for f:= 1 to max div s do
//        begin
//        assign(ts[f], 'snt' + str(f) + '.txt');
//        rewrite(ts[f]);
//        writeln('A');
//        readln;
//        clrscr;
//        end;
assign (t, 'so_nguyen_to.txt');
rewrite(t);
i:=2;
textcolor(lightred);
gotoxy(21,4);
write('Giai doan: 1');
textcolor(white);
while i<trunc(sqrt(max)) do
        begin
        //writeln(i);
        //readln;
        k:=k+1;
        while o[i] do i:= i+1;
        writeln(t, {k:9, '/ ', }i);
        gotoxy(1,7);
        writeln(k:9, '/ ', i:9, 100*i/max:10:5, '%');
        j:=1;
        //writeln(max div i);
        //readln;
        while j*i <= max do
                begin
                o[i*j]:=true;
                j:=j+1
                //writeln(j);
                //readln;
                end;
        end;
textcolor(lightblue);
gotoxy(21,4);
write('Giai doan: 2');
textcolor(white);
while i< max do
        begin
        i:=i+1;
        k:=k+1;
        while o[i] and (i<>max) do i:=i+1;
        //writeln(i);
        //readln;
        if not o[i] then
                begin
                writeln(t, {k:9, '/ ', }i);
                gotoxy(1,7);
                writeln(k:9, '/ ', i:9, 100*i/max:10:5, '%');
                end;
        end;
gotoxy(1,10);
textcolor(green);
if o[max] then k:= k-1;
close(t);
writeln('Xong!, Co ', k, ' duoi ', max, ' so da tim duoc!');
gotoxy(23,7);
textcolor(yellow);
writeln('100.0000%');
k:=0;
textcolor(blue);
repeat
        k:=k+1;
        if k=100 then k:=0;
        gotoxy(3,12);
        delay(1);
        {if k<10 then writeln('Nhan phim bat ky de thoat.  ');
        if (10<=k) and (k<20) then writeln('Nhan phim bat ky de thoat.. ');
        if 20<=k then writeln('Nhan phim bat ky de thoat...');}
        if k = 1 then writeln('Nhan phim bat ky de thoat.');
        if k =51 then writeln('                            ')
until keypressed;
//for f:= 1 to max div s do close(ts[f]);
end.
sử dụng: nhập số vào phần max rồi chạy
tạo tệp so_nguyen_to.txt
tạo với max=1 jack 5 triệu trong 12 phút (đã công bố với 100 triệu số rồi nên ko để đây)
bye.
 

Lê Duy Vũ

Học sinh tiến bộ
Thành viên
19 Tháng ba 2017
446
857
164
19
Thanh Hóa
Thanh Hóa
Chương trình này sử dụng "công nghệ" sàng Eratosthenes để tạo ra danh sách:
Mã:
program sang;
uses crt;
const max = nhap so vao;
var o: array [2..max] of boolean;
i, j, k: uint64;
t: text;
//ts: array [1..max div s] of text;
//f: byte;
{function st(_y:byte) : char;
        begin
        if _y=0 then st:='0';
        if _y=1 then st:='1';
        if _y=2 then st:='2';
        if _y=3 then st:='3';
        if _y=4 then st:='4';
        if _y=5 then st:='5';
        if _y=6 then st:='6';
        if _y=7 then st:='7';
        if _y=8 then st:='8';
        if _y=9 then st:='9';
        end;
function str(_x: uint64): string;
var _i: byte;
_xx: uint64;
        begin
        _xx:=_x;
        repeat
                str:=str+st(_xx mod 10);
                _xx:= _xx div 10;
        until _xx<10;
        end;}
begin
clrscr;
textcolor(red);
Writeln('Tao cac so nguyen to duoi ', max, '.');
textcolor(white);
gotoxy(5,3);
write('Nhan enter de tiep tuc.');
readln;
gotoxy(1,3);
Delline;
textcolor(green);
gotoxy(5,4);
writeln('Dang tinh toan:');
//for f:= 1 to max div s do
//        begin
//        assign(ts[f], 'snt' + str(f) + '.txt');
//        rewrite(ts[f]);
//        writeln('A');
//        readln;
//        clrscr;
//        end;
assign (t, 'so_nguyen_to.txt');
rewrite(t);
i:=2;
textcolor(lightred);
gotoxy(21,4);
write('Giai doan: 1');
textcolor(white);
while i<trunc(sqrt(max)) do
        begin
        //writeln(i);
        //readln;
        k:=k+1;
        while o[i] do i:= i+1;
        writeln(t, {k:9, '/ ', }i);
        gotoxy(1,7);
        writeln(k:9, '/ ', i:9, 100*i/max:10:5, '%');
        j:=1;
        //writeln(max div i);
        //readln;
        while j*i <= max do
                begin
                o[i*j]:=true;
                j:=j+1
                //writeln(j);
                //readln;
                end;
        end;
textcolor(lightblue);
gotoxy(21,4);
write('Giai doan: 2');
textcolor(white);
while i< max do
        begin
        i:=i+1;
        k:=k+1;
        while o[i] and (i<>max) do i:=i+1;
        //writeln(i);
        //readln;
        if not o[i] then
                begin
                writeln(t, {k:9, '/ ', }i);
                gotoxy(1,7);
                writeln(k:9, '/ ', i:9, 100*i/max:10:5, '%');
                end;
        end;
gotoxy(1,10);
textcolor(green);
if o[max] then k:= k-1;
close(t);
writeln('Xong!, Co ', k, ' duoi ', max, ' so da tim duoc!');
gotoxy(23,7);
textcolor(yellow);
writeln('100.0000%');
k:=0;
textcolor(blue);
repeat
        k:=k+1;
        if k=100 then k:=0;
        gotoxy(3,12);
        delay(1);
        {if k<10 then writeln('Nhan phim bat ky de thoat.  ');
        if (10<=k) and (k<20) then writeln('Nhan phim bat ky de thoat.. ');
        if 20<=k then writeln('Nhan phim bat ky de thoat...');}
        if k = 1 then writeln('Nhan phim bat ky de thoat.');
        if k =51 then writeln('                            ')
until keypressed;
//for f:= 1 to max div s do close(ts[f]);
end.
sử dụng: nhập số vào phần max rồi chạy
tạo tệp so_nguyen_to.txt
tạo với max=1 jack 5 triệu trong 12 phút (đã công bố với 100 triệu số rồi nên ko để đây)
bye.
dangxuanchuonMình nhớ là trong folder demo của Free Pascal có chương trình sàng Eratosthenes viết sẵn rồi. Độ phức tạp của cái này có NlogN thôi nên chạy testcase 10 củ chưa đến 1 phút. Hồi cấp 3 mình đi thi hsg toàn vào đây copy paste rồi chỉnh đầu ra với giá trị max cho phù hợp =)))
 

dangxuanchuon

Học sinh
Thành viên
31 Tháng bảy 2022
110
61
46
Phú Yên
Mình nhớ là trong folder demo của Free Pascal có chương trình sàng Eratosthenes viết sẵn rồi. Độ phức tạp của cái này có NlogN thôi nên chạy testcase 10 củ chưa đến 1 phút. Hồi cấp 3 mình đi thi hsg toàn vào đây copy paste rồi chỉnh đầu ra với giá trị max cho phù hợp =)))
Lê Duy Vũủa có hả, cho mình xin name (mình biết mở file qua FPC rồi nên khỏi chỉ)
 

Lê Duy Vũ

Học sinh tiến bộ
Thành viên
19 Tháng ba 2017
446
857
164
19
Thanh Hóa
Thanh Hóa
  • Like
Reactions: dangxuanchuon
Top Bottom