Skip to content

Commit 45aa3f4

Browse files
committed
Fix repunit divisibility edge case for prime 3
1 parent 140bffd commit 45aa3f4

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Maths/RepunitTheorem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* A repunit of length n is:
55
* R_n = (10^n - 1) / 9
66
*
7-
* For a prime p (p != 2, 5), p divides R_n iff ord_p(10) divides n.
7+
* For a prime p (p != 2, 3, 5), p divides R_n iff ord_p(10) divides n.
88
* Reference: https://en.wikipedia.org/wiki/Repunit
99
*/
1010

@@ -69,6 +69,7 @@ const isRepunitDivisibleByPrime = (length, prime) => {
6969

7070
const p = BigInt(prime)
7171
if (p === 2n || p === 5n) return false
72+
if (p === 3n) return BigInt(length) % 3n === 0n
7273
if (gcd(10n, p) !== 1n) return false
7374

7475
const order = multiplicativeOrder10(p)

Maths/test/RepunitTheorem.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ describe('RepunitTheorem', () => {
2121
})
2222

2323
it('returns false when divisibility condition does not hold', () => {
24+
expect(isRepunitDivisibleByPrime(1, 3n)).toBe(false)
25+
expect(isRepunitDivisibleByPrime(3, 3n)).toBe(true)
2426
expect(isRepunitDivisibleByPrime(6, 19n)).toBe(false)
2527
expect(isRepunitDivisibleByPrime(9, 2n)).toBe(false)
2628
expect(isRepunitDivisibleByPrime(9, 5n)).toBe(false)

0 commit comments

Comments
 (0)