#!/usr/bin/env groovy
print "\nYou want to know if ${args[0]} is prime? Let me think..."
n = Integer.parseInt(args[0]);
d = 2;
lim = Math.round(n / 2);
while (d < lim) {
if (n % d == 0) {
println "\nNope ${n} is evenly divisible by ${d}.";
System.exit(0);
}
d++;
if (d % 10 == 0) {
print ".";
}
}
println "\nYep! ${n} is a prime number!";
System.exit(0);
Just a place to put together some thoughts on Java, Technology and Other Stuff (tm) that interests me.
Monday, May 21, 2007
Some really quick Groovy code to test for a prime..
One of my kids had a homework assignment where they had to determine whether a given number was a prime number or not. They are pretty sharp kids and so they really didn't have a need for a programmatic answer but I decided to query my oldest son. He gave me a solution off-the-cuff but it wasn't complete so I wrote something simple in Groovy just to show to my one of other kids who was working on the homework assignment:
Subscribe to:
Posts (Atom)