Skip to content

R23-T055: Add extended_euclid trace and non-coprime doctests to chinese_remainder_theorem - #65

Open
code-paul-creator wants to merge 1 commit into
S-AI-Projects:masterfrom
code-paul-creator:R23-T055
Open

code-paul-creator wants to merge 1 commit into
S-AI-Projects:masterfrom
code-paul-creator:R23-T055

Conversation

@code-paul-creator

Copy link
Copy Markdown

Description

Traces extended_euclid(10, 6) and chinese_remainder_theorem(5, 1, 7, 3) step by step (by hand and with pdb) and turns the observations into doctests: intermediate results of the recursion, the gcd identity, and the documented behaviour for non-coprime moduli.

Related Issue

Fixes: #55

Type of Change

  • Bug Fix
  • New Feature
  • UI/UX Improvement
  • Performance Improvement
  • Tests
  • Documentation
  • Refactoring / Maintenance
  • CI/CD

Changes Made

  • Added doctests to extended_euclid: intermediate results (2,0)->(1,0), (4,2)->(0,1), (6,4)->(1,-1), the identity ax + by == gcd (2 for (10, 6)), and the a < b swap case (5, 7).
  • Added doctests to chinese_remainder_theorem: the result satisfies both congruences for coprime moduli, and a non-coprime input (6, 1, 4, 3) documented as unsupported.
  • Existing 8 doctests unchanged (21 total).
  • Added work/R23-T055/TRACE.md (tables of both traces, pdb steps, the surprise) and trace_driver.py.

Screenshots / Recordings

work/R23-T055/pdb_session.png

Testing

  • python -m pytest --doctest-modules maths/chinese_remainder_theorem.py -> passed (21 doctests)
  • ruff check maths/chinese_remainder_theorem.py -> all checks passed
  • pdb traces recorded in work/R23-T055/TRACE.md and checked by hand

Checklist

  • I have tested my changes locally.
  • I have added or updated tests, if needed.
  • I have updated documentation, if needed.
  • I have followed the project's coding guidelines.
  • I have run the relevant tests and checks.
  • I have addressed previous review feedback, if applicable.

Additional Notes

Finding: the existing doctest chinese_remainder_theorem(6, 1, 4, 3) -> 14 passes but 14 is not a valid solution (14 % 6 = 2, 14 % 4 = 2). 6 and 4 are not coprime, so extended_euclid(6, 4) gives ax + by = 2 instead of 1 and the formula breaks silently. A valid answer exists (7, modulo 12). Not fixed here (doctests only); documented in the new doctests and in work/R23-T055/TRACE.md. A follow-up could raise ValueError when gcd(n1, n2) != 1.

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.

Paulami Bhosle — R23-T055: Trace maths/chinese_remainder_theorem.py step by step

1 participant