> > |
%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 |