-
Notifications
You must be signed in to change notification settings - Fork 19.2k
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
Better way to check a number for Prime. Update PrimeCheck.java #5259
Conversation
The function does this by 1. Skip even numbers (except 2) since they are not prime. 2. Skip multiples of 3 after checking 3. 3. Start checking for factors from 5 and use the 6k ± 1 optimization. This is based on the fact that any prime number greater than 3 can be expressed as 6k ± 1. This helps to make it much more efficient for large numbers.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5259 +/- ##
============================================
- Coverage 51.25% 51.20% -0.05%
+ Complexity 3192 3191 -1
============================================
Files 524 524
Lines 15194 15204 +10
Branches 2893 2898 +5
============================================
- Hits 7787 7785 -2
- Misses 7084 7095 +11
- Partials 323 324 +1 ☔ View full report in Codecov by Sentry. |
Please, fix formatting issues, and update branch, and we can start review. |
I don't know how to do format. This is my first pull request. I will try
again soon
…On Thu, 8 Aug 2024 at 13:23, Alex Klymenko ***@***.***> wrote:
Please, fix formatting issues, and update branch, and we can start review.
—
Reply to this email directly, view it on GitHub
<#5259 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AYJIQWW4MUIGE5IBHCW53SLZQMPX7AVCNFSM6AAAAABJ7EVAS2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZVGE4DCMZTGY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Just run |
Done with manual elimination of the double spaces that were creating issues with the format.
I guess the code is good to go now. |
* @return true if the number is prime, false otherwise | ||
*/ | ||
public static boolean isPrimeNumberOptimised(long number) { | ||
// Numbers less than or equal to 1 are not prime |
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.
What do you think about removing these comments and moving them to the Javadoc? Because the code of this method is quite simple, but all these descriptions are harder to read: and after deleting it should be:
/**
* Determines if a given number is prime using an optimized approach.
* <p>
* This method checks for primality with the following steps:
* <ul>
* <li>Numbers less than or equal to 1 are not prime.</li>
* <li>Numbers 2 and 3 are prime.</li>
* <li>Eliminates even numbers and multiples of 3 early for efficiency.</li>
* <li>Uses the 6k ± 1 optimization for checking factors, which skips even numbers
* and multiples of 3 to cover all other possible factors.</li>
* <li>If no factors are found up to the square root of the number, the number is prime.</li>
* </ul>
*
* @param number the number to check
* @return true if the number is prime, false otherwise
*/
public static boolean isPrimeNumberOptimised(long number) {
if (number <= 1) return false;
if (number <= 3) return true;
if (number % 2 == 0 || number % 3 == 0) return false;
for (long i = 5; i * i <= number; i += 6) {
if (number % i == 0 || number % (i + 2) == 0) {
return false;
}
}
return true;
}
The function does this by
This helps to make it much more efficient for large numbers.
clang-format -i --style=file path/to/your/file.java