2008年12月16日星期二

output 0 to 1000 without loop and if, at least two methods.

1. Java version, recursive.

package shawn.test;
public class Thousand {
    public static void main(String[] args) {
        descrease2(1000);
    }
    public static Integer descrease2(Integer i) {
        try{
            Integer tmp = 1 / (i+1);
        }catch(Exception e){
            System.exit(1);
        }
        System.out.println(i--);
        descrease2(i);
        return i;
    }
}


2. Javascript Version, implementing Javascript Closure
<script type="text/javascript">
function test($num) {
  document.write($num+"<br />");
  $num--;
  return $num>0?test($num):document.write("0<br />");
}
var testMe = test(1000);
testMe();
</script>

3. Perl version, using language buildin looping feature.

#!/usr/local/bin/perl -w
@array = (0 .. 1000);
map {print $_, "\t"} @array;

> or

#!/usr/local/bin/perl -w
@array = (0 .. 1000);
grep {print $_, "\t"} @array;

> Extreme version, only 19 charactrors, no white space,
> no quotes, no brackets, no semicolon:

map{print$_}0..1000

没有评论: