Skip to content

Better way to check whether a number is prime or not. Update PrimeCheck.java #5258

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

Closed

Conversation

ZingadePrathamesh
Copy link
Contributor

Introduced one more function that checks whether the given long number is prime or not. This is to provide a more efficient and optimized way to check prime numbers, especially the large ones. The function does the following:

  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.
  • 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

Introduced one more function that checks whether the given long number is prime or not. This is to provide a more efficient and optimized way to check prime numbers, especially the large ones.
The function does the following:
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.
@codecov-commenter
Copy link

Codecov Report

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

Project coverage is 39.88%. Comparing base (7054535) to head (70f7326).

Files Patch % Lines
.../main/java/com/thealgorithms/maths/PrimeCheck.java 0.00% 6 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5258      +/-   ##
============================================
- Coverage     39.89%   39.88%   -0.01%     
+ Complexity     2450     2449       -1     
============================================
  Files           518      518              
  Lines         15484    15490       +6     
  Branches       2962     2965       +3     
============================================
+ Hits           6177     6178       +1     
- Misses         9017     9022       +5     
  Partials        290      290              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ZingadePrathamesh ZingadePrathamesh marked this pull request as draft June 27, 2024 04:54
@ZingadePrathamesh ZingadePrathamesh deleted the patch-1 branch June 27, 2024 05:00
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.

2 participants