Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions PrintPrimes.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
//A" - Dominic Daniel Kent - 260491325

public class PrintPrimes {
int numberOfPrimes;
int RR;
int CC;
int WW;
int ORDMAX;
int rowsPerPage;
int columnsPerPage;
int listOfPrimes[];

public PrintPrimes(int numberOfPrimes, int RR, int CC, int WW, int ORDMAX) {
public PrintPrimes(int numberOfPrimes, int rowsPerPage, int columnsPerPage) {
this.numberOfPrimes = numberOfPrimes;
this.RR = RR;
this.CC = CC;
this.WW = WW;
this.ORDMAX = ORDMAX;
this.rowsPerPage = rowsPerPage;
this.columnsPerPage =columnsPerPage;
this.listOfPrimes = new int[numberOfPrimes + 1];
}


public static void main(String[] args) {
PrintPrimes printPrimes = new PrintPrimes(300, 50, 4, 10, 30);
printPrimes.calculatePrimes();
printPrimes.printPrimes();
printPrimes.print();
}

public void calculatePrimes() {
/* Two is the only even prime. All other prime numbers are odd.
* To simplify the code, we simply add 2 as a prime number, and
* delegate the task of finding all odd prime numbers to a helper
* function.
*/
listOfPrimes[1] = 2;
calculateOddPrimes();
}

private void calculateOddPrimes() {
boolean JPRIME;
boolean isPRIME;
int N;
int MULT[] = new int[ORDMAX + 1];

Expand All @@ -51,34 +44,34 @@ private void calculateOddPrimes() {
}
N = 2;
JPRIME = true;
while (N < ORD && JPRIME) {
while (N < ORD && isPRIME) {
while (MULT[N] < J)
MULT[N] = MULT[N] + listOfPrimes[N] + listOfPrimes[N];
if (MULT[N] == J)
JPRIME = false;
isPRIME = false;
N = N + 1;
}
} while (!JPRIME);
} while (!isPRIME);
listOfPrimes[primesFoundSoFar] = J;
}
}

public void printPrimes() {
public void print() {
int PAGENUMBER = 1;
int PAGEOFFSET = 1;
while (PAGEOFFSET <= numberOfPrimes) {
System.out.println("The First " + numberOfPrimes +
" Prime Numbers --- Page " + PAGENUMBER);
System.out.println("");
for (int ROWOFFSET = PAGEOFFSET; ROWOFFSET < PAGEOFFSET + RR; ROWOFFSET++){
for (int C = 0; C < CC;C++)
if (ROWOFFSET + C * RR <= numberOfPrimes)
System.out.format("%10d", listOfPrimes[ROWOFFSET + C * RR]);
for (int ROWOFFSET = PAGEOFFSET; ROWOFFSET < PAGEOFFSET + rowsPerPage; ROWOFFSET++){
for (int column = 0; column < columnsPerPage ; column++)
if (ROWOFFSET + column * rowsPerPage <= numberOfPrimes)
System.out.format("%10d", listOfPrimes[ROWOFFSET + column * rowsPerPage]);
System.out.println("");
}
System.out.println("\f");
PAGENUMBER = PAGENUMBER + 1;
PAGEOFFSET = PAGEOFFSET + RR * CC;
PAGEOFFSET = PAGEOFFSET + rowsPerPage * columnsPerPage;
}
}
}
Expand Down