|
Groovy example source code file (nsieve.java)
The Groovy nsieve.java source code
/* The Great Computer Language Shootout
http://shootout.alioth.debian.org/
contributed by Alexei Svitkine
*/
public class nsieve
{
static int nsieve(int m, boolean[] isPrime)
{
for (int i=2; i <= m; i++) isPrime[i] = true;
int count = 0;
for (int i=2; i <= m; i++) {
if (isPrime[i]) {
for (int k=i+i; k <= m; k+=i) isPrime[k] = false;
count++;
}
}
return count;
}
public static String padNumber(int number, int fieldLen)
{
StringBuffer sb = new StringBuffer();
String bareNumber = "" + number;
int numSpaces = fieldLen - bareNumber.length();
for (int i = 0; i < numSpaces; i++)
sb.append(" ");
sb.append(bareNumber);
return sb.toString();
}
public static void main(String[] args)
{
int n = 2;
if (args.length > 0) n = Integer.parseInt(args[0]);
if (n < 2) n = 2;
int m = (1< |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.