X Tutup
The Wayback Machine - https://web.archive.org/web/20200609213745/https://github.com/TheAlgorithms/C-Plus-Plus/pull/834
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

Adding algorithm to check if number is prime or not. #834

Open
wants to merge 7 commits into
base: master
from

Conversation

@omkarlanghe
Copy link

omkarlanghe commented Jun 7, 2020

My first contribution to this repository contains algorithm to check if the given number is prime or not.

Small description on how I have optimized the logic:

Reduced all possibilities of a number which cannot be prime.

Eg: No even number, except 2 can be a prime number, hence we will increment our loop with i+2 jumping on all odd numbers only.

If number is <= 1 or if it is even except 2, break the loop and return false telling number is not prime.

@omkarlanghe omkarlanghe closed this Jun 7, 2020
@omkarlanghe omkarlanghe reopened this Jun 7, 2020
Copy link
Contributor

kvedala left a comment

  1. Use a simpler file name like ‘check_prime.cpp’
  2. See #807 for how to comment your code. People should be able to read and learn from your code
  3. Please done say ‘most optimized algorithm’ if there is no research publication backing your claim
@omkarlanghe omkarlanghe changed the title Adding optimized algorithm to check if number is prime or not. Adding algorithm to check if number is prime or not. Jun 7, 2020
@omkarlanghe
Copy link
Author

omkarlanghe commented Jun 7, 2020

@kvedala Modified the code as per the changes you have suggested.

@kvedala
Copy link
Contributor

kvedala commented Jun 7, 2020

@kvedala Modified the code as per the changes you have suggested.

Please see the files changes in #807 - The comment blocks, and structure is per the doxygen standard. link to actual file

GCC suggests -

$ g++ math/check_prime.cpp -o test -Wall
math/check_prime.cpp: In function 'int is_prime(int)':
math/check_prime.cpp:8:23: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
    8 |     } else if (no & 1 == 0) {  // otherwise if other even numbers, return false

Also, if you want to optimize further, you can have the line# 12 loop condition to go only till (I*i) < no - again, this already exists in some of the existing code in the repository.

math/check_prime.cpp Outdated Show resolved Hide resolved
@kvedala
kvedala approved these changes Jun 7, 2020
Copy link
Contributor

kvedala left a comment

good efforts. Documentation is still not upto standard - not all functions are documented. Please see #807 as referenced above.

@kvedala
kvedala approved these changes Jun 7, 2020
Copy link
Contributor

kvedala left a comment

👍

math/check_prime.cpp Outdated Show resolved Hide resolved
math/check_prime.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

ayaankhan98 left a comment

looks good

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

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.
X Tutup