This is a Prime Numbers’ Generator from Integers up to 2,000,000,000, written in Turbo Pascal.
This program generates the prime numbers up to a given arithmetic limit, using 4 (four) different known methods. They are all based in modulo algebra. The methods are:

  1. This uses the mathematical definition of a prime number to generate them. A prime is only divided by 1 and himself. Disadvantage: The method is very slow.
  2. In this method, each number N is modulo divided only with the half of the numbers below it (N/2). Thus, the search is becoming faster than the first method.
  3. In the third method, a number, let N, is modulo divided only with the numbers up to sqrt{N}. As a result of that, the generation process is 10 times faster than the previous methods.
  4. The fourth method is the best of all. It is based on the 3rd method, only it bypass the sqrt{x} praxis, by modulo dividing each number N with the first, the second and the third prime number and then, if there is a residue, it modulo divides N with j, where j is an integer from j*j cdots N with step 2. The above trick has the eye-blink effect: the method generates primes (within a reasonable range) before you blink your eyes….:-)


The program is algorithmically very simple. In other words, there are limitations originating in the LONGINT Type of Turbo Pascal, which has a range of -2,147,xxx,xxx to 2,147,xxx,xxx. As a result, expect the program to terminate abnormally if you enter an upper Limit more than 2 billion…

program protoi2;
   uses crt;
   label nocode, try, again, sexit;
   	var
   p,number,i,j, select, count, position : longint;
   ans, choice,  dmode, algo    : char;
   code : string;
 
function root ( n : longint) : longint;
begin
 root := trunc(sqrt(n));
end;
 
function prime1(n: longint) : integer;
var j : longint;
begin
 for j:=2 to n-1 do
  begin
    p:= n mod j;
   if p=0 then begin prime1:=0; exit; end;
  end;
  prime1:=1 ;
end;
 
function prime2(n: longint) : integer;
var j,k : longint;
begin
 k:=trunc(n/2);
 for j:=2 to k  do
  begin
    p:= n mod j;
    if p=0 then begin prime2:=0; exit; end;
  end;
  prime2:=1 ;
end;
 
function prime3(n: longint) : integer;
var j,k : longint;
begin
 k:=root(n);
 for j:=2 to k do
  begin
    p:= n mod j;
    if p=0 then begin prime3:=0; exit; end;
  end;
  prime3:=1 ;
end;
 
function prime4(n: longint) : integer;
var j : longint;
begin
  if (n mod 2) = 0 then
  if  ( n=2 ) then begin prime4:=1; exit; end
  else begin prime4:=0;exit; end;
  if (n mod 3) = 0 then
  if ( n=3 ) then begin prime4:=1; exit; end
  else begin prime4:=0;exit; end;
  if (n mod 5) = 0 then
  if  n=5  then begin prime4:=1; exit; end
  else begin prime4:=0;exit; end;
 j:=7;
 while j*j <=  n do
  begin
    p:= n mod j;
    if p=0 then begin prime4:=0; exit; end;
    j:=j+2;
  end;
  prime4:=1 ;
end;
 
 
begin
   clrscr;
   writeln(' version 0.1');
   writeln;
   writeln('THIS program estimates the prime numbers...'); writeln;
 
     algo:='4';  dmode:='2';
 
      while 1=1 do
      begin
  writeln;
  writeln('|=- MENU -=|');
  writeln('1. Find all the Primes until a given Limit.');
  writeln('2. Find the N-nth Prime.');
  writeln('3. Options.');
  writeln('4. Exit.');
  write('>');
  readln(choice);
  writeln;
 
  case choice of
  '4':    begin  goto sexit; end;
  '3':    begin
      writeln('Select an Algorithm: ');
      writeln('   1...Slower / Lamez');
      writeln('   2...Medium / Andrew');
      writeln('   3...Fast / Makis');
      writeln('   4...Eye-Blink');
      write('>');
      readln(algo);
      writeln;
      writeln('Select Output Mode: ');
      writeln('   1...Horizontal');
      writeln('   2...Vertical');
      write('>');
      readln(dmode);
        writeln('Changing something, please wait....');
        for j:=1 to 30 do
        begin
        Delay(500);
        write('.');
        end;
        writeln;
        writeln('Good things need hard work to get!'); writeln;
        writeln('Squeeze a button, please');
        readln;
    end;
  '2':   begin
        count:=0;
        writeln('Enter No of the Prime:');
        readln(position);
        case algo of
        '4':    begin
          for i:=2 to 2000000000 do
      begin
       if prime4(i)=1 then  count:=count+1;
       if count=position then  break;
       gotoxy(1, wherey);
       write('\');
       gotoxy(1, wherey);
       write(' ');
       gotoxy(1, wherey);
       write('-');
       gotoxy(1, wherey);
       write(' ');
       gotoxy(1, wherey);
       write('/');
       gotoxy(1,wherey);
       write(' ');
       gotoxy(1, wherey);
       write('|');
 
       gotoxy(1,wherey);
       write(' ');
 
       end;
 
      writeln ('Prime No #',position,'  is: ',i);
         end;
 
 
        '3':    begin
          for i:=2 to 2000000000 do
      begin
       if prime3(i)=1 then  count:=count+1;
       if count=position then  break;
       gotoxy(1, wherey);
       write('\');
       gotoxy(1, wherey);
       write(' ');
       gotoxy(1, wherey);
       write('-');
       gotoxy(1, wherey);
       write(' ');
       gotoxy(1, wherey);
       write('/');
       gotoxy(1,wherey);
       write(' ');
       gotoxy(1, wherey);
       write('|');
 
       gotoxy(1,wherey);
       write(' ');
 
       end;
 
      writeln ('Prime No #',position,'  is: ',i);
         end;
 
 
 
        '2':    begin
          for i:=2 to 2000000000 do
      begin
       if prime2(i)=1 then  count:=count+1;
       if count=position then  break;
       gotoxy(1, wherey);
       write('\');
       gotoxy(1, wherey);
       write(' ');
       gotoxy(1, wherey);
       write('-');
       gotoxy(1, wherey);
       write(' ');
       gotoxy(1, wherey);
       write('/');
       gotoxy(1,wherey);
       write(' ');
       gotoxy(1, wherey);
       write('|');
 
       gotoxy(1,wherey);
       write(' ');
 
       end;
 
      writeln ('Prime No #',position,'  is: ',i);
         end;
 
 
 
         '1':    begin
          for i:=2 to 2000000000 do
      begin
       if prime1(i)=1 then  count:=count+1;
       if count=position then  break;
       gotoxy(1, wherey);
       write('\');
       gotoxy(1, wherey);
       write(' ');
       gotoxy(1, wherey);
       write('-');
       gotoxy(1, wherey);
       write(' ');
       gotoxy(1, wherey);
       write('/');
       gotoxy(1,wherey);
       write(' ');
       gotoxy(1, wherey);
       write('|');
 
       gotoxy(1,wherey);
       write(' ');
 
       end;
 
      writeln ('Prime No #',position,'  is: ',i);
         end;
 
      end;
 
  end;
   '1':     begin
      writeln('Enter a number for Prime-Generator upper Limit');
      readln(number);
      writeln;
      writeln('Prime Numbers up to: ', number);
      count:=0;
      case algo of
      '1':    begin
       for i:=2 to number do
       begin
        if prime1(i)=1 then
     begin
       if dmode='1' then
          begin
          write(',',i); count:=count+1;
          end
        else
         begin
          count:=count+1;
          write('Prime = ');
          writeln(i);
         end;
      end;
        end;
      end;
 
       '2':begin
     for i:=2 to number do
      begin
        if prime2(i)=1 then
         begin
          if dmode='1' then
       begin
         write(',',i); count:=count+1;
       end
     else
       begin
         count:=count+1;
         write('Prime = ');
         writeln(i);
       end;
         end;
       end;
         end;
 
        '3' :    begin
     for i:=2 to number do
       begin
         if prime3(i)=1 then
          begin
     if dmode='1' then
       begin
        write(',',i); count:=count+1;
       end
     else
       begin
        count:=count+1;
        write('Prime = ');
       writeln(i);
       end;
         end;
       end;
         end;
 
        '4':    begin
     for i:=2 to number do
       begin
         if prime4(i)=1 then
     begin
      if dmode='1' then
       begin
        write(',',i); count:=count+1;
       end
      else
        begin
         count:=count+1;
         write('Prime = ');
         writeln(i);
        end;
          end;
       end;
        end;
        else   begin
     for i:=2 to number do
       begin
 
         if prime4(i)=1 then
     begin
      if dmode='1' then
       begin
        write(',',i); count:=count+1;
       end
      else
        begin
         count:=count+1;
         write('Prime = ');
         writeln(i);
        end;
          end;
       end;
        end;
       end;
 
        writeln;
        writeln('Found ', count,' prime numbers');
 
   end;
 
  end;
 
 
 
      end;
 
 
sexit:  writeln ('Bye....');
  readln;
 
end.