Nice Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For nice
Ackermann's Function
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a ackermann.jar ackermann

To run:
   java -jar ackermann.jar 8
*/


void main(String[] args){

   // NOTE: the type of n will be 
   //       inferred by the compiler

   let n = toSingleInt(args);   
   println("Ack(3," + n + "): " + ack(3,n)); 
}


int ack(int m, int n){
   if (m == 0) return n + 1;
   if (n == 0) return ack(m-1, 1);
   return ack(m-1, ack(m, n-1)); 
}


int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }
Array Access
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a ary3.jar ary3

To run:
   java -jar ary3.jar 7000
*/


// NOTE: the types of n,x,y,i,k,j will be 
//       inferred by the compiler


import ackermann; // reuse toSingleInt

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

   let x = new int[n];
   for(var i=0; i=0; j--) y[j] += x[j];

   print(y[0]); print(" "); println(y[n-1]);
}
Count Lines/Words/Chars
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a wc.jar wc

To run:
   java -jar wc.jar < input.txt > out.txt
*/


import java.io.*;

void main(String[] args){
   let int newline = 0xA;
   let int space = 0x20;
   let int cr = 0xD;
   let int tab = 0x9;

   int value, nl = 0, nw = 0, nc = 0;
   try {
      boolean insideWord = false;
      BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
      while ((value = r.read()) != -1){
         ++nc;
         if (value == newline) ++nl;
         if (value == space || value == cr || value == newline || value == tab) 
            insideWord = false;
         else if (!insideWord) {
            insideWord = true;
            ++nw;
         }
      }
   } catch (IOException e) { System.err.println(e); }

   print(nl); print(" "); print(nw); print(" "); println(nc);
}
Exception Mechanisms
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a except.jar except

To run:
   java -jar except.jar 20000
*/


import ackermann; // reuse toSingleInt


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

   for(var i=0; i
Fibonacci Numbers
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a fibo.jar fibo

To run:
   java -jar fibo.jar 32
*/


import ackermann; // reuse toSingleInt


void main(String[] args){
    println( fib( toSingleInt(args) ) ); 
}


int fib(int n){ 
   if (n < 2) return 1; else return fib(n-2) + fib(n-1);
}
Hash (Associative Array) Access
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a hash.jar hash

To run:
   java -jar hash.jar 80000
*/


import ackermann; // reuse toSingleInt


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

   HashMap table = new HashMap();

   for (int i = 1; i <= n; i++) 
      table.put( toString(i, 16), i );
   for (int i = n; i > 0; i--)
      if (table.get( toString(i, 10) ) != null) count++;

   println(count);
}


String toString(int, int) = 
   native String java.lang.Integer.toString(int, int);


Hashes, Part II
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a hash2.jar hash2

To run:
   java -jar hash2.jar 150
*/


import ackermann; // reuse toSingleInt


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

   HashMap table1 = new HashMap(nKeys);
   HashMap table2 = new HashMap();

   for (var i = 0; i <= nKeys; i++) 
      table1.put( "foo_" + toString(i), new Cell(value: i) );

   String key;
   int v1;        
   ?Cell c2;      // c2 = table2.get(key) can be null 

   while (n-- > 0) {
      Iterator> item = table1.entrySet().iterator();
      while(item.hasNext()) {
         Map.Entry e = item.next();
         key = e.getKey();
         v1 = e.getValue().value;                       
         if ( (c2 = table2.get(key)) != null)
            c2.value += v1;   
         else 
            table2.put(key, new Cell(value: v1) );
      }
   }
   print( toString( table1.get("foo_1") )); print(" ");
   print( toString( table1.get("foo_9999") )); print(" ");
   print( toString( table2.get("foo_1") )); print(" ");
   println( toString( table2.get("foo_9999") )); 
}


class Cell { int value; }

String toString(?Cell c) {
   if (c==null) return "null"; else return toString(c.value);
}
Heapsort
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

   Transliterated from the Java implementation

To compile:	
   nicec --sourcepath=.. -d=. -a heapsort.jar heapsort

To run:
   java -jar heapsort.jar 80000
*/


import random; // reuse gen_random & floatFormat & toSingleInt


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

   let numbers = new double[n+1];
   for (var i = 1; i <= n; i++) numbers[i] = gen_random(1);

   heapsort(n, numbers);

   println(floatFormat(10).format(numbers[n]));
}


void heapsort(int n, double[] ra) {
   int l, j, ir, i;
   double rra;

   if (n < 2) return;
   l = (n >> 1) + 1;
   ir = n;
   for (;;) {
      if (l > 1) rra = ra[--l];
      else {
         rra = ra[ir];
         ra[ir] = ra[1];
         if (--ir == 1) {
            ra[1] = rra;
            return;
         }
      }
      i = l;
      j = l << 1;
      while (j <= ir) {
         if (j < ir && ra[j] < ra[j+1]) { ++j; }
         if (rra < ra[j]) {
            ra[i] = ra[j];
            j += (i = j);
         } 
         else j = ir + 1;
      }
      ra[i] = rra;
   }
}
Hello World
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

   Transliterated from the Java implementation

To compile:	
   nicec --sourcepath=.. -a hello.jar hello

To run:
   java -jar hello.jar
*/


void main(String[] args){ 
   println("hello world"); 
}
List Operations
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a lists.jar lists

To run:
   java -jar lists.jar 16
*/


import ackermann; // reuse toSingleInt


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

   let nSize = 10000;
   int L1Count = 0;

   while (n-- > 0){
      // initialize L1
      ArrayList L1 = new ArrayList(nSize);
      for (var j = 1; j <= nSize; j++) L1.add(j);

      // copy L1 to L2
      ArrayList L2 = L1.clone();

      // remove from left of L2 add to right of L3
      ArrayList L3 = new ArrayList(nSize);
      while (L2.size() > 0) 
         L3.add( L2.removeAt(0) ); 

      // remove from right of L3 add to right of L2
      int index;
      while ( (index = L3.size()) > 0) 
         L2.add( L3.removeAt(index - 1) );

      // reverse L1
      Collections.reverse(L1);

      // check that first item is now SIZE
      // NOTE: no Object to int type cast needed

      if (L1[0] != nSize) println("First item of L1 != SIZE");

      // check that L1 == L2
      if ( !L1.equals(L2) ) println("L1 != L2");
      L1Count = L1.size();
      }

   println(L1Count);
}
Matrix Multiplication
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

   Transliterated from the Java implementation

To compile:	
   nicec --sourcepath=.. -d=. -a matrix.jar matrix

To run:
   java -jar matrix.jar 300
*/


// NOTE: the type of a variable declared with let
//       or var will be inferred by the compiler


import ackermann; // reuse toSingleInt


void main(String[] args){
   var n =  toSingleInt(args);
   let int SIZE = 30;

   let m1 = mkmatrix(SIZE, SIZE);
   let m2 = mkmatrix(SIZE, SIZE);
   let mm = new int[SIZE][SIZE];

   while (n-- > 0) mmult(m1, m2, mm);

   print(mm[0][0]); print(" ");
   print(mm[2][3]); print(" ");      
   print(mm[3][2]); print(" ");
   println(mm[4][4]);
}


int[][] mkmatrix(int nRows, int nCols) {
   int count = 1;
   let m = new int[nRows][nCols];
   for (var i = 0; i < nRows; i++) 
      for (var j = 0; j < nCols; j++) 
         m[i][j] = count++;
   return m;
}


void mmult(int[][] m1, int[][] m2, int[][] m) {
   let nRows = m1.length;
   let nCols = nRows; // Assume a square matrix
   for (var i=0; i < nRows; i++) 
      for (var j = 0; j < nCols; j++) {
         int val = 0;
         for (var k = 0; k < nCols; k++) 
            val += m1[i][k] * m2[k][j];
         m[i][j] = val;
      }
}
Method Calls
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a methcall.jar methcall

To run:
   java -jar methcall.jar 1000000
*/


import ackermann; // reuse toSingleInt


void main(String[] args){
   let n =  toSingleInt(args);
   boolean val;

   Toggle toggle = new Toggle();
   for(var i=1; i= t.toggleTrigger){
      t.state = !t.state;
      t.count = 0;
   }
   return t; 
}



Nested Loops
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a nestedloop.jar nestedloop

To run:
   java -jar nestedloop.jar 16
*/


import ackermann; // reuse toSingleInt


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

   for(int a=0; a
Object Instantiation
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a objinst.jar objinst

To run:
   java -jar objinst.jar 1000000
*/


import methcall; // reuse Toggle & NToggle & toSingleInt


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

   Toggle toggle = new Toggle();
   for(var i=0; i<5; i++) println( toggle.activate().value() );
   for(var i=0; i
Random Number Generator
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a random.jar random

To run:
   java -jar random.jar 900000
*/


import java.text.*;

import ackermann; // reuse toSingleInt


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

   while (n-- > 0) result = gen_random(100.0);

   println(floatFormat(9).format(result));
}


let int IM = 139968;
let int IA = 3877;
let int IC = 29573;
var int seed = 42;


double gen_random(double max) {        
   seed = (seed * IA + IC) % IM;
   return( max * seed / IM );
}


NumberFormat floatFormat(int digits){
   NumberFormat f = NumberFormat.getInstance();
   f.setGroupingUsed(false);
   f.setMaximumFractionDigits(digits);
   f.setMinimumFractionDigits(digits);
   return f;
} 
Sieve of Erathostenes
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a sieve.jar sieve

To run:
   java -jar sieve.jar 900
*/


// NOTE: the type of constants & variables declared with 
//       let & var will be inferred by the compiler


import ackermann; // reuse toSingleInt


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

   let start = 2;
   let stop = 8192;
   var 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); 
}
Spell Checker
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a spellcheck.jar spellcheck

To run:
   java -jar spellcheck.jar < input.txt > out.txt
*/


import java.io.*;


void main(String[] args){
   HashMap dictionary = new HashMap();

   try {
      BufferedReader f = new BufferedReader(new FileReader("Usr.Dict.Words"));
      f.foreach(String word => { dictionary.put(word, 1); });
      f.close();
   } 
   catch (IOException e) { 
      System.err.println(e); 
      return; 
   }

   try {
      BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
      r.foreach(String word => { 
         if (!dictionary.containsKey(word)) println(word); });
   } 
   catch (IOException e) { 
      System.err.println(e); 
   }
}


void foreach(BufferedReader r, String -> void expr) { 
   ?String s;
   while ((s = r.readLine()) != null) expr(s);
}
Statistical Moments
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a moments.jar moments

To run:
   java -jar moments.jar < input.txt > out.txt
*/


import java.io.*;
import java.text.*;

import random; // reuse floatFormat
import sumcol; // reuse bufferedStdInReader()


void main(String[] args){
   double num, sum = 0;
   ArrayList numbers = new ArrayList();
   try {
      BufferedReader reader = bufferedStdInReader();
      ?String line;
      while ((line = reader.readLine()) != null){
         num = Double.parseDouble(line);
         sum += num; 
         numbers.add(num);
      };
   } 
   catch (IOException e) { 
      System.err.println(e); 
   }

   double mean = 0.0;
   double average_deviation = 0.0;
   double standard_deviation = 0.0;
   double variance = 0.0;
   double skew = 0.0;
   double kurtosis = 0.0;
   double median = 0.0;
   double deviation = 0.0;
   int i, n, mid = 0;

   n = numbers.size();
   mean = sum/n;
   for (i=0; i
String Concatenation
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a strcat.jar strcat

To run:
   java -jar strcat.jar 40000
*/


import ackermann; // reuse toSingleInt


void main(String[] args){
   var n = toSingleInt(args);
   String s = "hello\n";
   StringBuffer buffer = new StringBuffer(32);

   while (n-- > 0) buffer.append(s);

   println(buffer.length()); 
}
Sum a Column of Integers
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a sumcol.jar sumcol

To run:
   java -jar sumcol.jar < input.txt > out.txt
*/


import java.io.*;

void main(String[] args){
   int sum = 0;

   try {
      BufferedReader reader = bufferedStdInReader();
      ?String line;
      while (( line = reader.readLine()) != null)
         sum += Integer.parseInt(line);
   } 
   catch (IOException e) { 
      System.err.println(e); 
   }

   println(sum);
}


BufferedReader bufferedStdInReader() = 
   new BufferedReader(new InputStreamReader(System.in));