Nice TWiki > Doc > SieveBeginnerExample (r1.1 vs. r1.2) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
 <<O>>  Difference Topic SieveBeginnerExample (r1.2 - 27 Aug 2003 - IsaacGouy)
Changed:
<
<

/* Please try the BeginnerExamples? before the NoviceExamples?

>
>

/* Please try the NoviceExamples? before the BeginnerExamples?

Added:
>
>

%META:TOPICMOVED{by="IsaacGouy" date="1062001937" from="Doc.SieveNoviceExample" to="Doc.SieveBeginnerExample"}%


 <<O>>  Difference Topic SieveBeginnerExample (r1.1 - 27 Aug 2003 - IsaacGouy)
Added:
>
>

%META:TOPICINFO{author="IsaacGouy" date="1061947020" format="1.0" version="1.1"}% %META:TOPICPARENT{name="CodeExamples"}%

/* 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 . { View | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.1 - 27 Aug 2003 - 01:17 GMT - IsaacGouy
Revision r1.2 - 27 Aug 2003 - 16:34 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.