Skip to content

Commit cac9e3d

Browse files
committed
Added solution for Project Euler problem 129. Fixes: #2695
1 parent c9500dc commit cac9e3d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

project_euler/problem_129/__init__.py

Whitespace-only changes.

project_euler/problem_129/sol1.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
A number consisting entirely of ones is called a repunit. We shall define R(k) to be
3+
a repunit of length k; for example, R(6) = 111111.
4+
5+
Given that n is a positive integer and GCD(n, 10) = 1, it can be shown that there
6+
always exists a value, k, for which R(k) is divisible by n, and let A(n) be the least
7+
such value of k; for example, A(7) = 6 and A(41) = 5.
8+
9+
The least value of n for which A(n) first exceeds ten is 17.
10+
11+
Find the least value of n for which A(n) first exceeds one-million.
12+
"""
13+
14+
15+
def A(n: int) -> int:
16+
"""
17+
Return the least value k such that the Repunit of length k is divisible by n.
18+
>>> A(7)
19+
6
20+
>>> A(41)
21+
5
22+
>>> A(1234567)
23+
34020
24+
"""
25+
if n % 5 == 0 or n % 2 == 0:
26+
return 0
27+
R = 1
28+
k = 1
29+
while R:
30+
R = (10 * R + 1) % n
31+
k += 1
32+
return k
33+
34+
35+
def solution(limit: int = 1000000) -> int:
36+
"""
37+
Return the least value of n for which A(n) first exceeds limit.
38+
"""
39+
n = limit - 1
40+
while A(n) <= limit:
41+
n += 2
42+
return n
43+
44+
45+
if __name__ == "__main__":
46+
print(solution())

0 commit comments

Comments
 (0)