forked from mcgill-ecse321/examples-primes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintPrimes.java
108 lines (95 loc) · 3.42 KB
/
PrintPrimes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* This class calculates a number of primes
* using a sieve no higher than a set order,
* and returning no more than a set amount
* of prime numbers.
*
* @author Michael Williams (206369438)
*/
public class PrintPrimes {
int numberOfPrimes;
int rowsPerPage;
int columns;
int maxOrder;
int listOfPrimes[];
int listOfNonPrimes[];
public PrintPrimes(int numberOfPrimes, int rowsPerPage, int columns, int placeholder, int maxOrder) {
this.numberOfPrimes = numberOfPrimes;
this.rowsPerPage = rowsPerPage;
this.columns = columns;
this.maxOrder = maxOrder;
this.listOfPrimes = new int[numberOfPrimes];
this.listOfNonPrimes = new int[maxOrder + 1];
}
public static void main(String[] args) {
PrintPrimes printPrimes = new PrintPrimes(300, 50, 4, 10, 30);
printPrimes.calculatePrimes();
printPrimes.printPrimes();
}
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[0] = 2;
calculateOddPrimes();
}
private boolean isPrime(int order, int val) {
/* Helper function that checks if a number is a prime,
* while updating the list of nonPrimes.
*/
int n = 2;
boolean prime;
prime = true;
while (n < order && prime) {
while (listOfNonPrimes[n] < val) {
listOfNonPrimes[n] = listOfNonPrimes[n] + listOfPrimes[n - 1] + listOfPrimes[n - 1];
}
if (listOfNonPrimes[n] == val) {
prime = false;
}
n++;
}
return prime;
}
private void calculateOddPrimes() {
/* Helper method that calculates the odd primes using a sieve.
* Actual checking if a number is a prime occurs in the isPrime function
*/
int j = 1;
int order = 2;
int square = 9;
for(int primesFoundSoFar = 1; primesFoundSoFar < numberOfPrimes; primesFoundSoFar++) {
do {
j += 2;
if (j == square) {
order++;
square = listOfPrimes[order - 1] * listOfPrimes[order - 1];
listOfNonPrimes[order - 1] = j;
}
} while (!isPrime(order,j));
listOfPrimes[primesFoundSoFar] = j ;
}
}
public void printPrimes() {
int pageNumber = 1;
int pageOffset = 0;
while (pageOffset <= numberOfPrimes) {
System.out.println("The First " + numberOfPrimes +
" Prime Numbers --- Page " + pageNumber);
System.out.println("");
for (int rowOffset = pageOffset; rowOffset < pageOffset + rowsPerPage; rowOffset++){
for (int col = 0; col < columns;col++) {
if (rowOffset + col * rowsPerPage < numberOfPrimes) {
System.out.format("%10d", listOfPrimes[rowOffset + col * rowsPerPage]);
}
}
System.out.println("");
}
System.out.println("\f");
pageNumber++;
pageOffset = pageOffset + rowsPerPage * columns;
}
}
}