Skip to content

Instantly share code, notes, and snippets.

@carlosdiaz
Created February 5, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosdiaz/8fa749a8fe4b629ac1b8 to your computer and use it in GitHub Desktop.
Save carlosdiaz/8fa749a8fe4b629ac1b8 to your computer and use it in GitHub Desktop.
public class PrimeNumbersList{
public static void main(String args[]) {
System.out.println("Starting...");
List<Integer> list = computePrimes(20);
for(Integer object: list) {
System.out.println(object);
}
}
public static List<Integer> computePrimes(int max) {
List<Integer> list = new ArrayList<Integer>();
for (int i=1; i<=max; i++) {
//System.out.println("Checking i" +i);
if (isPrime(i)) {
System.out.println("Value added:" + i);
list.add(i);
}
}
return list;
}
public static boolean isPrime(int value) {
int counter = 0;
boolean returnValue = true;
for (int i=1; i<=value; i++) {
if (value % i ==0) {
counter ++;
}
if (counter>2) {
returnValue = false;
break;
}
}
return returnValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment