Nice TWiki > Doc > CodeExamples > SieveBeginnerExample (r1.1) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the BeginnerExamples before the NoviceExamples
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

Topic SieveBeginnerExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.1 - 27 Aug 2003 - 01:17 GMT - IsaacGouy
Parents: WebHome > CodeExamples
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.