Skip to content
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

Closed

Conversation

ZingadePrathamesh
Copy link

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.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

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-commenter
Copy link

codecov-commenter commented Jun 27, 2024

Codecov Report

Attention: Patch coverage is 0% with 10 lines in your changes missing coverage. Please review.

Project coverage is 51.20%. Comparing base (fa22317) to head (07368ad).

Files with missing lines Patch % Lines
.../main/java/com/thealgorithms/maths/PrimeCheck.java 0.00% 10 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

@alxkm
Copy link
Contributor

alxkm commented Aug 8, 2024

Please, fix formatting issues, and update branch, and we can start review.

@ZingadePrathamesh
Copy link
Author

ZingadePrathamesh commented Aug 9, 2024 via email

@vil02
Copy link
Member

vil02 commented Aug 9, 2024

I don't know how to do format. This is my first pull request. I will try again soon

Just run clang-format. If you are having problems with that, please inspect the log:
https://github.com/TheAlgorithms/Java/actions/runs/9691003340/job/26741793012?pr=5259
It seams that you just need to remove some double spaces in few places - it can definitely be done manually.

@ZingadePrathamesh ZingadePrathamesh marked this pull request as draft August 14, 2024 07:15
ZingadePrathamesh and others added 4 commits August 14, 2024 12:50
Done with manual elimination of the double spaces that were creating issues with the format.
@ZingadePrathamesh ZingadePrathamesh marked this pull request as ready for review August 14, 2024 07:22
@ZingadePrathamesh
Copy link
Author

I don't know how to do format. This is my first pull request. I will try again soon

Just run clang-format. If you are having problems with that, please inspect the log: https://github.com/TheAlgorithms/Java/actions/runs/9691003340/job/26741793012?pr=5259 It seams that you just need to remove some double spaces in few places - it can definitely be done manually.

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
Copy link
Contributor

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants