-
Notifications
You must be signed in to change notification settings - Fork 60
Refactoring changes #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Refactoring changes #129
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,20 +58,35 @@ public static boolean isPrime(int n) { | |
} | ||
return SmallPrimes.millerRabinPrimeTest(n); | ||
} | ||
|
||
/** | ||
* Return the number which is not a multiple of 3. | ||
* | ||
* @param n Positive number. | ||
* @return number which is not a multiple of 3. | ||
* @throws IllegalArgumentException if n < 0. | ||
*/ | ||
public static int nonMultipleOf3(int n) { | ||
final int remainder = n % 3; | ||
if (0 == remainder) { // if n % 3 == 0 | ||
n += 2; // n % 3 == 2 | ||
} else if (1 == remainder) { // if n % 3 == 1 | ||
n += 4; // n % 3 == 2 | ||
} | ||
return n; | ||
} | ||
/** | ||
* Return the smallest prime greater than or equal to n. | ||
* | ||
* @param n Positive number. | ||
* @return the smallest prime greater than or equal to {@code n}. | ||
* @throws IllegalArgumentException if n < 0. | ||
*/ | ||
public static int nextPrime(int n) { | ||
if (n < 0) { | ||
throw new IllegalArgumentException(MessageFormat.format(NUMBER_TOO_SMALL, n, 0)); | ||
} | ||
if (n <= 2) { | ||
return 2; | ||
int firstPrime = 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating a variable does not add anything to the code. This could all be done using a comment. Note that numbers allows the |
||
return firstPrime; // 2 is first possible smallest prime greater than or equal to n, n here can be 0, 1 or 2 | ||
} | ||
n |= 1; // make sure n is odd | ||
|
||
|
@@ -81,12 +96,7 @@ public static int nextPrime(int n) { | |
|
||
// prepare entry in the +2, +4 loop: | ||
// n should not be a multiple of 3 | ||
final int rem = n % 3; | ||
if (0 == rem) { // if n % 3 == 0 | ||
n += 2; // n % 3 == 2 | ||
} else if (1 == rem) { // if n % 3 == 1 | ||
n += 4; // n % 3 == 2 | ||
} | ||
n = nonMultipleOf3(n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is refactoring for the sake of refactoring. The code comments explain what is occurring. The comments here also match the comments for the |
||
while (true) { // this loop skips all multiple of 3 | ||
if (isPrime(n)) { | ||
return n; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous benchmark had a single variable
type
in each@State
class. By splitting this into two you will cause JMH to run the benchmark with more variables. JMH creates benchmarks as the cross-product of all variables. Thus one of the states will now have1 * 2
benchmarks where previously it would be 1. This is a waste of resources. It will also create two columns in the results where the two columns have redundant information as child classes use only one of the variables.