Prime solution in C++ incorrectly returns true for some composite numbers
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 25/100
Research direction
Locate the C++ solution containing isPrime and inspect the loop that checks possible divisors. Confirm the behavior with the cases 9 and 25; done means both are identified as composite. An open linked pull request (#39) already addresses this issue.
Written by the indexing model from the issue text.
Description
Describe the bug
The isPrime function in the C++ solution incorrectly identifies some composite numbers as prime.
The issue is with the loop condition i * i < n. Since it uses < instead of <=, the loop stops before checking the square root of n. For example, 9 and 25 are incorrectly considered prime because their square roots (3 and 5) are never checked as potential divisors.
Expected behavior
The function should correctly identify composite numbers like 9 and 25 as non-prime.
Fix
Changing the loop condition from i * i < n to i * i <= n ensures that divisors up to and including the square root of n are checked.
I would like to work on the fix.
- Dominant language
- Python
- Stars
- 1
- Forks
- 6
- Avg merge
- 17h 52m
- Merged PRs (30d)
- 14
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from techcsispit/code-corner
-
prime/solution.java doesn't compile: public class PrimeCheck in solution.java, README requires Solution.java with a Solution classPossibly taken @tanujb03 claimed this 2 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 82/100
techcsispit/code-corner#35 ·
Maintainers usually reply within 1 day
-
Failed compilations always report 0.000 secondsPossibly taken @Shivangi-singh18 claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
techcsispit/code-corner#37 · 1 comment ·
Maintainers usually reply within 1 day
-
run.py: Python solutions are skipped on Windows when run via python instead of python3Possibly taken @tanujb03 claimed this 2 days ago. Openbug good first issue
Difficulty 1/5 Under an hour Newbie friendliness 15/100
techcsispit/code-corner#29 ·
Maintainers usually reply within 1 day
-
fibonacci/solution.c: prints negative numbers for n of 47 or more (F(90) prints -1581614984)Possibly taken @tanujb03 claimed this 2 days ago. Openbug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 25/100
techcsispit/code-corner#27 ·
Maintainers usually reply within 1 day
-
binary-search/Solution.java: prints -1 when the target is the last element (`9 1 3 5 7 9`) or in a 1-element list (`5 5`)Possibly taken @tanujb03 claimed this 2 days ago. Openbug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 22/100
techcsispit/code-corner#26 ·
Maintainers usually reply within 1 day
All issues in techcsispit/code-corner
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
openwisp/openwisp-utils#777 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
google-research/weatherbenchX#310 ·
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
huggingface/peft#3955 ·
Maintainers usually reply within 1 day
-
author-page correction
Difficulty 2/5 1-3 hours Newbie friendliness 60/100
acl-org/acl-anthology#10313 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
AOSSIE-Org/Gitcord-GithubDiscordBot#147 · 1 comment ·
Maintainers usually reply within 1 day