I made this program to find primes and twin pairs. I'd like feedback on anything from formatting to content to technique. In short: how would this be different if it was written by an experienced professional?
The program runs five methods:
- getMax asks for an integer input which is the maximum number below which to find primes and twin prime pairs. It also has an error message to catch unacceptable input and ask again.
- sieve checks every number between 0 and max and returns a boolean array which says whether each index is prime or not.
- primesCount counts many primes there are, and prints it.
- primesList creates an integer array of all primes, and prints it.
- pairsCount counts how many twin prime pairs there are, and prints it.
- pairsList creates a string array of all twin prime pairs, and prints it.
Code:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int max = getMax(in);
boolean[] primesSieve = sieve(max);
int primesCount = primesCount(primesSieve);
int[] primesList = primesList(primesSieve, primesCount);
int pairsCount = pairsCount(primesList);
pairsList(primesList, pairsCount);
}
// User input for maximum number to sieve
public static int getMax(Scanner in) {
System.out.print("Maximum number to sieve: ");
while (!in.hasNextInt()) {
String input = in.next();
System.out.println("Error: \"" + input + "\" is not a number. Try again.");
System.out.print("Maximum number to sieve: ");
}
int max = in.nextInt();
return max;
}
// Create boolean array of prime status of integers less than max
public static boolean[] sieve(int max) {
// Create initial array and assume all are primes
boolean[] arePrimes = new boolean[max];
for (int i = 0; i<max; i++) {
arePrimes[i] = true;
}
// Mark multiples of each prime as non-prime
for (int i = 2; i<max; i++) {
if (arePrimes[i]) {
for (int m = 2; i * m < max; m++) {
arePrimes[i*m] = false;
}
}
}
return arePrimes;
}
// Count and print how many primes are less than max
public static int primesCount(boolean[] primes) {
int count = 0;
for (boolean prime : primes) {
if (prime) {
count++;
}
}
System.out.println("Number of primes: " + count);
return count;
}
// Create and print array listing all primes less than max
public static int[] primesList(boolean[] sievedPrimes, int numberOfPrimes) {
int[] listPrimes = new int[numberOfPrimes-2];
int n = 0;
for (int i=2; i<sievedPrimes.length; i++) {
if (sievedPrimes[i]) {
listPrimes[n] = i;
n++;
}
}
System.out.println("List of primes: " + Arrays.toString(listPrimes));
return listPrimes;
}
// Count and print the number of twin prime pairs
public static int pairsCount(int[] listOfPrimes) {
int pairsCount = 0;
for (int i=0; i<listOfPrimes.length-1; i++) {
if (listOfPrimes[i+1] - listOfPrimes[i] == 2) {
pairsCount++;
}
}
System.out.println("Number of twin prime pairs: " + pairsCount);
return pairsCount;
}
// Create and print array listing all pairs of twin primes less than max
public static String[] pairsList(int[] listOfPrimes, int pairsCount) {
String[] twinPrimes = new String[pairsCount];
int n = 0;
for (int i=0; i<listOfPrimes.length-1; i++) {
if (listOfPrimes[i+1] - listOfPrimes[i] == 2) {
twinPrimes[n] = listOfPrimes[i] + " and " + listOfPrimes[i+1];
n++;
}
}
System.out.println("List of twin prime pairs: " + Arrays.toString(twinPrimes));
return twinPrimes;
}
Arrays.fill
method to initialize the array withtrue
. \$\endgroup\$