//------------------------------------------------------
// This XJEase program prints out prime numbers to 1000.
// It uses a 1000 bit number to store the results.
//
// This file can be inserted as an additional code file in any XJEase/XJDeveloper project.
//
// (c)2002-2006 XJTAG Limited
//
// Disclaimer: XJTAG makes no guarantees whatsoever
// about this code.  You use it at your own risk ...
//
// If you find any problems with this file, please
// contact support@xjtag.com
//------------------------------------------------------

//----------------------------------------------------------------------
// Constants
//----------------------------------------------------------------------

CONST INT MAXPRIME := 1000;
CONST INT SQRMAX   := 32;   // approx. square root of 1000

//----------------------------------------------------------------------
// Main entry point
//----------------------------------------------------------------------

CircuitTest()()

  INT numbers WIDTH MAXPRIME; // Array of numbers
  INT i;
  INT startPos := 0;

  PRINT("\n\nCalculates prime number up to ",MAXPRIME,"\n");

  numbers := 0;
  numbers[0]:= 1; // Set zero not to be prime.
  numbers[1]:= 1; // Set one not to be prime.

  DO
    // Find the next number which isn't set on the list.
    DO
      startPos := startPos + 1;
    WHILE (numbers[startPos])
    END;

    PRINT("Prime Found : ",startPos,"\n");

    // Now set all multiples of that number, because they can't be prime.
    i := startPos;
    DO
      i := i + startPos;
      numbers[i] := 1;
    WHILE (i < MAXPRIME)
    END;
  WHILE (startPos < SQRMAX)
  END;

  // Find all numbers that haven't been set, as they must be prime.
  i := startPos;
  DO
    i := i + 1;
    IF numbers[i] = 0 THEN PRINT("Prime Found : ",i,"\n"); END;
  WHILE (i < MAXPRIME)
  END;

END;