Quote:
public static void main(String[] args) {
int start = 0;
int end = 51;
ArrayList al = PrimeNumber.extractPrime(start, end);
System.out.println("prime beetwen " + start + " and " + end + " are: ");
// loop to reveal result
for (int counter = 0; counter < al.size(); ++counter) {
System.out.println((Integer) al.get(counter));
}
}
Working Class: Quote:
public class PrimeNumber {
public static ArrayList extractPrime(int _start, int _end) throws IllegalArgumentException {
// validation
if (_end < _start) {
throw new IllegalArgumentException("invalid argument");
}
// validation
if ((_start < 0) || (_end < 0)) {
throw new IllegalArgumentException("invalid argument");
}
// this ArrayList contains all primary numbers
ArrayList arrayList = new ArrayList();
int factor = 0;
int counter = 0;
int number = 0;
for (number = _start; number < _end; ++number) {
if (number > 1) {
for (counter = 1; counter <= number; ++counter) {
// find the factor
if (number % counter == 0) {
++factor;
}
}
// prime number has only two factors, 1 and the number it self
if (factor == 2) {
arrayList.add(number);
}
// reset the factor
factor = 0;
}
}
return (arrayList);
}
}