-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeCalculator.java
More file actions
169 lines (150 loc) · 4.07 KB
/
Copy pathPrimeCalculator.java
File metadata and controls
169 lines (150 loc) · 4.07 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* This class provides helpful methods for getting prime numbers, testing
* if a given number is prime, and getting a list of the prime factors of
* a given number. Note: prime numbers are integers greater than one with
* no positive divisors besides one and itself.
*/
import java.util.ArrayList;
public class PrimeCalculator {
// Class variable
private int primeNum;
/**
* Constructor allowing the client to begin the object with a number already
* stored in the instance variable.
* @param initial The number to assign the instance variable to
*/
public PrimeCalculator(int initial){
this.primeNum = initial;
}
/**
* No argument constructor. Initializes the instance variable to zero.
*/
public PrimeCalculator(){
this.primeNum = 0;
}
/**
* Calculates the next prime number that is greater than the object's
* instance variable.
* @return The next larger prime number.
*/
public int getNextPrime(){
if (this.primeNum == 0 || this.primeNum == 1){
this.primeNum++;
if (this.primeNum == 1){
this.primeNum++;
}
return this.primeNum;
}
this.primeNum++;
if (isPrime(this.primeNum)){
return this.primeNum;
} else {
while (! isPrime(this.primeNum)){
this.primeNum++;
}
return this.primeNum;
}
}
/**
* Calculates and returns the next higher prime number based on a starting
* number n.
* @param n The beginning number to get the next higher prime number to.
* @return The next higher prime number.
*/
public int getNextHigherPrime(int n){
if (n < 0){
n = 0;
}
if (isPrime(n)){
n++;
if (n == 1){
n++;
}
}
while (! isPrime(n)){
n++;
}
return n;
}
/**
* Tests if a given number n is prime or not.
* @param n The number to test if it is prime or not
* @return True if the number is prime
*/
public boolean isPrime(int n){
if (n < 0){
return false;
}
for (int i = 2; i < n; i++){
if (n % i == 0){
return false;
}
}
return true;
}
/**
* The getListOfPrimes method fills and returns a list of n prime numbers.
* @param n The number of prime numbers the client wishes to retrieve
* @return An ArrayList of prime numbers
*/
public ArrayList<Integer> getListOfPrimes(int n){
ArrayList<Integer> listOfPrimes = new ArrayList<Integer>();
PrimeCalculator pNumber = new PrimeCalculator();
while (n > 0){
int number = pNumber.getNextPrime();
if (isPrime(number)){
listOfPrimes.add(number);
n--;
}
}
return listOfPrimes;
}
/**
* The getPrimeFactors method returns a list of prime factors for a given
* parameter variable.
* @param num The number to get the prime factors of
* @return An ArrayList of prime factors
*/
public ArrayList<Integer> getPrimeFactors(int num){
ArrayList<Integer> primeList = new ArrayList<Integer>();
if (isPrime(num)){
primeList.add(num);
primeList.add(1);
return primeList;
} else {
getFactors(primeList,num);
return primeList;
}
}
/**
* The getFactors method fills a parameter ArrayList variable with the
* prime factors of the given int parameter variable.
* @param primeList The list to add the prime factors to
* @param num The integer to find the prime factors of
*/
private void getFactors(ArrayList<Integer> primeList, int num) {
if (isPrime(num)) {
primeList.add(num);
} else {
int divisor = 2;
if (num % divisor == 0) {
int quotient = num / divisor;
primeList.add(divisor);
getFactors(primeList,quotient);
} else {
while (num % divisor != 0) {
divisor++;
}
int quotient = num / divisor;
primeList.add(divisor);
if (! isPrime(quotient)){
getFactors(primeList,quotient);
} else {
if (isPrime(quotient)) {
primeList.add(quotient);
}
}
}
}
}
}