-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path026.py
More file actions
73 lines (59 loc) · 1.79 KB
/
026.py
File metadata and controls
73 lines (59 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# COMPLETED
"""
Problem 26
==========
A unit fraction contains 1 in the numerator. The decimal representation of
the unit fractions with denominators 2 to 10 are given:
1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can
be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d < 1000 for which ^1/[d] contains the longest recurring
cycle in its decimal fraction part.
Answer: 6aab1270668d8cac7cef2566a1c5f569
"""
from typing import DefaultDict
from common import check
PROBLEM_NUMBER = 26
ANSWER_HASH = "6aab1270668d8cac7cef2566a1c5f569"
DECIMALS = 2000
MAX_NUMBER = 1000
def accurate_divide(d, decimals):
return f"0.{10 ** decimals // d}"
def calculate(d):
decimal = accurate_divide(d, DECIMALS)
for i, c_i in enumerate(decimal):
if i == 0:
continue
for j, c_j in enumerate(decimal[:i]):
if c_j == c_i:
pattern = decimal[j:i]
if len(pattern) > len(decimal) - i:
continue
valid = True
for k, c_k in enumerate(decimal[i:]):
if c_k != pattern[k % len(pattern)]:
valid = False
break
if valid:
return pattern
return None
max_d = None
max_length = 0
max_pattern = None
for d in range(1, MAX_NUMBER):
pattern = calculate(d)
if pattern is None:
continue
if len(pattern) > max_length:
max_length = len(pattern)
max_d = d
max_pattern = pattern
check(max_d, PROBLEM_NUMBER, ANSWER_HASH)