
(********************************************************************)
(*                                                                  *)
(*  prime.sd7     Write a list of prime numbers                     *)
(*  Copyright (C) 1990, 1991, 1992, 1993, 1994, 2004  Thomas Mertes *)
(*                                                                  *)
(*  This program is free software; you can redistribute it and/or   *)
(*  modify it under the terms of the GNU General Public License as  *)
(*  published by the Free Software Foundation; either version 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program is distributed in the hope that it will be useful, *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
(*  GNU General Public License for more details.                    *)
(*                                                                  *)
(*  You should have received a copy of the GNU General Public       *)
(*  License along with this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


$ include "seed7_05.s7i";
  include "stdio.s7i";
  include "keybd.s7i";

var integer: position is 0;


const func boolean: isPrime (in integer: number) is func
  result
    var boolean: prime is FALSE;
  local
    var integer: count is 7;
  begin
    if odd(number) then
      if number rem 3 <> 0 then
        if number rem 5 <> 0 then
          while number rem count <> 0 and count * count <= number do
            count +:= 4;
            if number rem count <> 0 and count * count <= number then
              count +:= 2;
            end if;
          end while;
          prime := number rem count <> 0;
        end if;
      end if;
    end if;
  end func; (* isPrime *)


const proc: main is func
  local
    var integer: number is 0;
  begin
    write("       2       3       5       7");
    position := 4;
    number := 11;
    repeat
      if isPrime(number) then
        write(str(number) lpad 8);
        incr(position);
        if position = 9 then
          writeln;
          position := 0;
        end if;
      end if;
      incr(number);
    until keypressed(KEYBOARD);
    writeln;
  end func;
