Chapter 3, Exercise 9, Thinking in Scala
Integer.parseInt is from java.lang.Integer. I guess i could have used the "tfor" loop that I defined here, if I wanted this to look more like Java or more like what the exercise statement was asking for.
object c3x9 {
def main ( args: Array[String] ) = {
val limit = Integer.parseInt( args(0) );
var cand = 2;
while (cand < limit) {
var is_prime = true;
var test = 2;
while (is_prime && test*test <= cand ) {
if ( (cand % test) == 0 ) {
is_prime= false;
}
else {
test = test +1;
}
}
if (is_prime)
Console.println( cand + " is prime." );
/*
else
Console.println( cand + " is not prime." );
*/
cand = cand +1;
}
}
}
8:13:31 AM