TWiki . Doc . SieveBeginnerExample

/* Please try the NoviceExamples before the BeginnerExamples
To compile:
   nicec --sourcepath=.. -a sieve.jar sieve
To run:
   java -jar sieve.jar 900
*/


void main(String[] args){
   var n = toSingleInt(args);

   let start = 2;
   let stop = 8192;
   let isPrime = new boolean[stop+1];
   var count = 0;

   while (n-- > 0){ 
      count = 0;
      for(var i=start; i <= stop; i++) isPrime[i] = true;
      for(var i=start; i <= stop; i++) 
         if(isPrime[i]) {
             // remove all multiples of prime: i
            for(var k=i+i; k <= stop; k+=i) isPrime[k] = false;
            count++;
         }
   }
   println("Count: " + count); 
}


int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }


/* Notes - language
Compare with Java at the "Win32 Language Shootout"

   let start = 2;
Declare constant start, the compiler will infer the constant type
from the literal value.

   let isPrime = new boolean[stop+1];
Declare and initialize array isPrime, the compiler will infer 
the type from the initial assignment. Identifiers declared with 
keyword let can be assigned a value only once. 

   var count = 0;
Declare and initialize count, the compiler will infer 
the type from the initial assignment. Identifiers declared with 
keyword var can be assigned a value multiple times.
*/
-- IsaacGouy - 27 Aug 2003

----- Revision r1.3 - 10 Jan 2004 - 15:44 GMT - IsaacGouy
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.