Skip to content

Add maths/repunit.py: repunit divisibility theorem#14699

Closed
ltianyi992 wants to merge 1 commit into
TheAlgorithms:masterfrom
ltianyi992:fix/issue-13999-feature-the-repunit-theorem
Closed

Add maths/repunit.py: repunit divisibility theorem#14699
ltianyi992 wants to merge 1 commit into
TheAlgorithms:masterfrom
ltianyi992:fix/issue-13999-feature-the-repunit-theorem

Conversation

@ltianyi992
Copy link
Copy Markdown

What does this add?

Implements the repunit divisibility theorem in maths/repunit.py.

A repunit R(n) = 111...1 (n ones). The key theorem:

A prime p ≠ 2, 5 divides R(n) if and only if n is a multiple of
ord_p(10) — the multiplicative order of 10 modulo p.

This gives an O(log p) divisibility check that never needs to construct the
(potentially millions-of-digits) repunit.

Functions added

Function Description
multiplicative_order(base, modulus) Smallest k ≥ 1 s.t. base^k ≡ 1 (mod modulus)
repunit_length_for_prime(prime) Smallest n s.t. prime | R(n)
is_repunit(n) True iff n consists solely of digit 1
repunit_divisible_by(n, prime) O(log prime) check, no giant number needed

Example

>>> repunit_divisible_by(6, 7)
True   # 111111 / 7 = 15873 exactly
>>> repunit_divisible_by(3, 7)
False  # 111 is not divisible by 7

Type hints & doctests

All functions include full type hints and passing doctests (30 tests total).

Closes #13999

References

Implements four functions for the repunit divisibility theorem:
- multiplicative_order(base, modulus): smallest k s.t. base^k ≡ 1 (mod modulus)
- repunit_length_for_prime(prime): smallest n s.t. prime | R(n)
- is_repunit(n): True iff n consists solely of digit 1
- repunit_divisible_by(n, prime): O(log prime) check, no need to build R(n)

Closes TheAlgorithms#13999
@algorithms-keeper
Copy link
Copy Markdown

Closing this pull request as invalid

@ltianyi992, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines.

If you're facing any problem on how to mark a checkbox, please read the following instructions:

  • Read a point one at a time and think if it is relevant to the pull request or not.
  • If it is, then mark it by putting a x between the square bracket like so: [x]

NOTE: Only [x] is supported so if you have put any other letter or symbol between the brackets, that will be marked as invalid. If that is the case then please open a new pull request with the appropriate changes.

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed invalid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: The Repunit theorem

1 participant