Skip to content

Instantly share code, notes, and snippets.

@robgmerrill
Created January 22, 2020 19:51
Show Gist options
  • Save robgmerrill/0ea06278811c4b7d96a263746bdae584 to your computer and use it in GitHub Desktop.
Save robgmerrill/0ea06278811c4b7d96a263746bdae584 to your computer and use it in GitHub Desktop.
Prime Directive
// some other stuff
import java.util.ArrayList;
class PrimeDirective {
public boolean isPrime(int number) {
if (number == 2) {
return true;
} else if (number < 2) {
return false;
}
for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
public ArrayList<Integer> onlyPrimes(int[] numbers) {
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int number : numbers) {
if (isPrime(number)) {
primes.add(number);
}
}
return primes;
}
public static void main(String[] args) {
PrimeDirective pd = new PrimeDirective();
int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};
System.out.println(pd.onlyPrimes(numbers));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment