Skip to content

Conversation

Zhanbingli
Copy link

What does this PR do?

Adds all_diophantine_solutions(a: int, b: int, c: int, n: int = 2) -> list[tuple[int, int]]
to blockchain/diophantine_equation.py. It returns up to n integer solutions to
the linear Diophantine equation a*x + b*y = c, using extended_gcd.

Why is this needed?

  • Follows project guidelines (return values, no prints).
  • Improves usability: callers can programmatically get multiple solutions.
  • Includes type hints, clear docstring, doctests, and proper errors.

How has this been tested?

  • Doctests included in the function’s docstring.
  • Ran locally:
    • python3 -m doctest -v blockchain/diophantine_equation.py
    • pre-commit run --all-files
    • ruff check

Implementation details

  • If g = gcd(a, b) does not divide c, raises ValueError.
  • Computes one particular solution from extended_gcd, then enumerates general
    solutions: x = x0 + t*(b/g), y = y0 - t*(a/g) for t = 0..n-1.
  • Time complexity: O(log(max(|a|,|b|))) for base solution + O(n) enumeration.
  • Space complexity: O(1) besides the returned list.

Breaking changes?

None.

Dependencies added/removed?

None.

Related issues / references

N/A (can add Closes #<id> if there’s a related issue).

Checklist

  • I read the project’s CONTRIBUTING guidelines.
  • My code follows the style rules (pre-commit / ruff).
  • I added/kept type hints for public functions.
  • I added doctests (and/or unit tests) covering normal & edge cases.
  • I documented time/space complexity in the docstring.
  • File is in the correct directory and imports remain intact.

Copy link
Author

@Zhanbingli Zhanbingli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the blockchain

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