Skip to content

Fix wrong isPrime results, an EOF loop in the sprint4 map reader and a mis-decoded morse colon - #125

Merged
krotname merged 1 commit into
mainfrom
fix/obvious-bugs
Aug 16, 2026
Merged

Fix wrong isPrime results, an EOF loop in the sprint4 map reader and a mis-decoded morse colon#125
krotname merged 1 commit into
mainfrom
fix/obvious-bugs

Conversation

@krotname

Copy link
Copy Markdown
Owner

Summary

  • kyu6.Prime.isPrime returned true for composite numbers. It called BigInteger.isProbablePrime((int) Math.log(num) + 1), so the certainty was derived from the number itself: for 9 the certainty is 3, i.e. Miller-Rabin was allowed to be wrong with probability up to 1/2^3. isPrime(9) reported true on roughly 2% of runs — a non-deterministic kata answer. Replaced with deterministic trial division (num < 2 → false, even numbers handled, odd divisors up to sqrt(num)), which is also faster for int.
  • algorithms.sprint4.Map.Reader replayed its buffer after EOF. read() refilled only when ptr == len; at EOF len becomes -1 with ptr == 0, so the condition was false and the method kept returning stale bytes from the previous buffer (and eventually ran past its end). ptr >= len makes EOF sticky.
  • nextCommand() could spin forever. Its whitespace skip loop had no EOF check, unlike nextInt(), so a truncated input (n larger than the number of commands actually supplied) left it looping on -1 forever. It now throws IOException("Unexpected end of input"), which main already treats as rejected input.
  • Morse ---... decoded to ;. ---... is a colon; the semicolon -.-.-. was already mapped correctly one line above, so : could never be decoded. Also removed two leftover debug entries in the reverse table ("a" → "b", "c" → "d").
  • The diamond kata emitted CRLF on Windows. GiveMeDiamond.print joined rows with System.lineSeparator(); the kata expects \n. TowerBuilder.show() was left alone — it is a display helper, not the kata answer.
  • dependabot.yml had two entries targeting master, a branch that does not exist in this repository (default is main), so they could only ever error. Removed; the main entries for maven and github-actions stay.

Not changed (needs a decision)

  • algorithms/sprint0/Utils.printList swallows IOException per element. There is an explicit test (UtilsTest.printListSwallowsWriterFailures) asserting exactly that, so the behaviour is deliberate; changing it means changing the signature and the test.
  • algorithms/sprint6/DorogayaSet uses -1 as a "not found" sentinel, which collides with a legal answer. Fixing it changes the public method contract.
  • Surefire only includes **/*Test.java, **/*Tests.java, **/*TestCase.java and **/*IT.java, so the tag-based suites in src/test/java/quality/*Suite.java never run in the build. Wiring them in would duplicate every test run — a decision about how the suites are meant to be used.

Test plan

  • mvn -B -ntp verify (JDK 21) — BUILD SUCCESS, Tests run: 684, Failures: 0, Errors: 0, Skipped: 0, including checkstyle, PMD, SpotBugs and the 70% JaCoCo gates.
  • New assertions: PrimeTest now checks primes (2, 3, 5, 7, 11, 13, 101, 7919, 2147483647) and non-primes (-7, -1, 0, 1, 4, 9, 15, 25, 49, 121, 7917, 2147483645); the old code failed on 9 intermittently.
  • New MapTest.readerReportsEndOfInputInsteadOfReplayingTheBuffer feeds "g 7" on System.in and asserts that the next nextCommand()/nextInt() throw instead of replaying the buffer; it carries @Timeout(10) so the old infinite loop would fail rather than hang the build.
  • MorseCodeDecoderTest gained the case "---... -.-.-."":;".

🤖 Generated with Claude Code

…at EOF

- replace the probabilistic isPrime certainty with trial division
- make the Reader EOF sticky and fail fast in nextCommand
- decode "---..." as a colon and drop two junk reverse-morse entries
- emit "\n" from the diamond kata instead of the platform separator
- drop the dependabot entries targeting the non-existent master branch

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@krotname
krotname merged commit 0d93c31 into main Aug 16, 2026
14 checks passed
@krotname
krotname deleted the fix/obvious-bugs branch August 16, 2026 10:03
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.

1 participant