Program Pascal : Bilangan Prima

Program Bil_prim2;
uses wincrt;

var prima:array[1..500] of boolean;
    batasan,batas,I,J:integer;
begin
Writeln('batas maksimal sampai dengan angka 500 ');
Write('masukkan batasan bilangan prima =  ');
Readln(batasan);
     for I:=1 to batasan do
         prima[I]:=true;
         batas:=trunc(sqrt(batasan));

         I:=2;
         while I<=batas do
               begin
                    if prima[I] then
                       begin
                            J:=I+I;
                            while J<=batasan do
                                  begin
                                       prima[J]:=false;
                                       J:=J+I
                                  end
                       end;
                    I:=I+1
               end;
         Writeln('Bilangan prima antara 1 s/d',batasan:1);
         writeln('----------------------------');
         Writeln;
         J:=1;
         for I:=1 to batasan do
             begin
             if prima[I] then
                begin
                     if J>8 then
                        begin
                        J:=1;
                        writeln;
                        end
                     else
                         begin
                         write('   ',I:3);
                         J:=J+1;
                         end;
                end;
             end;
         Writeln;
         Writeln;
         Writeln('selesai');
         end.

1 comments: