-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: add repunit theorem helpers #1892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nickzerjeski
wants to merge
4
commits into
TheAlgorithms:master
Choose a base branch
from
nickzerjeski:feat-repunit-theorem-1866
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /** | ||
| * Repunit theorem helpers. | ||
| * | ||
| * A repunit of length n is: | ||
| * R_n = (10^n - 1) / 9 | ||
| * | ||
| * For a prime p (p != 2, 3, 5), p divides R_n iff ord_p(10) divides n. | ||
| * Reference: https://en.wikipedia.org/wiki/Repunit | ||
| */ | ||
|
|
||
| const gcd = (a, b) => { | ||
| let x = BigInt(a) | ||
| let y = BigInt(b) | ||
| while (y !== 0n) { | ||
| ;[x, y] = [y, x % y] | ||
| } | ||
| return x < 0n ? -x : x | ||
| } | ||
|
|
||
| const modPow = (base, exp, mod) => { | ||
| let result = 1n | ||
| let b = BigInt(base) % BigInt(mod) | ||
| let e = BigInt(exp) | ||
| const m = BigInt(mod) | ||
|
|
||
| while (e > 0n) { | ||
| if (e & 1n) result = (result * b) % m | ||
| b = (b * b) % m | ||
| e >>= 1n | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| const multiplicativeOrder10 = (prime) => { | ||
| const p = BigInt(prime) | ||
| if (p <= 1n) throw new RangeError('prime must be > 1') | ||
| if (gcd(10n, p) !== 1n) throw new RangeError('10 and prime must be coprime') | ||
|
|
||
| // For prime p, ord_p(10) divides p-1. | ||
| const upper = p - 1n | ||
| for (let k = 1n; k <= upper; k++) { | ||
| if (upper % k === 0n && modPow(10n, k, p) === 1n) { | ||
| return k | ||
| } | ||
| } | ||
|
nickzerjeski marked this conversation as resolved.
|
||
|
|
||
| throw new Error('multiplicative order not found') | ||
| } | ||
|
|
||
| const repunitMod = (length, mod) => { | ||
| if (!Number.isInteger(length) || length < 1) { | ||
| throw new RangeError('length must be a positive integer') | ||
| } | ||
| const m = BigInt(mod) | ||
| if (m <= 0n) throw new RangeError('mod must be > 0') | ||
|
|
||
| let remainder = 0n | ||
| for (let i = 0; i < length; i++) { | ||
| remainder = (remainder * 10n + 1n) % m | ||
| } | ||
| return remainder | ||
| } | ||
|
|
||
| const isRepunitDivisibleByPrime = (length, prime) => { | ||
| if (!Number.isInteger(length) || length < 1) { | ||
| throw new RangeError('length must be a positive integer') | ||
| } | ||
|
|
||
| const p = BigInt(prime) | ||
| if (p === 2n || p === 5n) return false | ||
| if (p === 3n) return BigInt(length) % 3n === 0n | ||
| if (gcd(10n, p) !== 1n) return false | ||
|
|
||
| const order = multiplicativeOrder10(p) | ||
| return BigInt(length) % order === 0n | ||
|
nickzerjeski marked this conversation as resolved.
|
||
| } | ||
|
|
||
| export { multiplicativeOrder10, repunitMod, isRepunitDivisibleByPrime } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { | ||
| isRepunitDivisibleByPrime, | ||
| multiplicativeOrder10, | ||
| repunitMod | ||
| } from '../RepunitTheorem' | ||
|
|
||
| describe('RepunitTheorem', () => { | ||
| it('computes multiplicative order examples', () => { | ||
| expect(multiplicativeOrder10(11n)).toBe(2n) | ||
| expect(multiplicativeOrder10(37n)).toBe(3n) | ||
| expect(multiplicativeOrder10(7n)).toBe(6n) | ||
| }) | ||
|
|
||
| it('checks repunit divisibility using the theorem', () => { | ||
| // 111111 is divisible by 3, 7, 11, 13, 37 | ||
| expect(isRepunitDivisibleByPrime(6, 3n)).toBe(true) | ||
| expect(isRepunitDivisibleByPrime(6, 7n)).toBe(true) | ||
| expect(isRepunitDivisibleByPrime(6, 11n)).toBe(true) | ||
| expect(isRepunitDivisibleByPrime(6, 13n)).toBe(true) | ||
| expect(isRepunitDivisibleByPrime(6, 37n)).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false when divisibility condition does not hold', () => { | ||
| expect(isRepunitDivisibleByPrime(1, 3n)).toBe(false) | ||
| expect(isRepunitDivisibleByPrime(3, 3n)).toBe(true) | ||
| expect(isRepunitDivisibleByPrime(6, 19n)).toBe(false) | ||
| expect(isRepunitDivisibleByPrime(9, 2n)).toBe(false) | ||
| expect(isRepunitDivisibleByPrime(9, 5n)).toBe(false) | ||
| }) | ||
|
nickzerjeski marked this conversation as resolved.
|
||
|
|
||
| it('computes repunit modulo without building huge integers', () => { | ||
| expect(repunitMod(6, 37n)).toBe(0n) | ||
| expect(repunitMod(6, 11n)).toBe(0n) | ||
| expect(repunitMod(7, 13n)).toBe(1n) | ||
| }) | ||
|
|
||
| it('validates multiplicative order input', () => { | ||
| expect(() => multiplicativeOrder10(1n)).toThrow(RangeError) | ||
| expect(() => multiplicativeOrder10(10n)).toThrow(RangeError) | ||
| }) | ||
|
|
||
| it('validates repunitMod input', () => { | ||
| expect(() => repunitMod(0, 7n)).toThrow(RangeError) | ||
| expect(() => repunitMod(5, 0n)).toThrow(RangeError) | ||
| }) | ||
|
|
||
| it('validates repunit divisibility input length', () => { | ||
| expect(() => isRepunitDivisibleByPrime(0, 7n)).toThrow(RangeError) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file header comment says the theorem holds for primes p != 2,5, but it also needs p != 3 (equivalently gcd(p,9)=1). As written, it documents an incorrect condition and matches the buggy behavior for p=3.