-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem26.py
47 lines (41 loc) · 1.35 KB
/
problem26.py
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
from datetime import datetime
from decimal import Decimal, getcontext
def get_recurring(fraction):
fraction = str(fraction)[2::]
fraction_length = len(fraction)
counter, offset = 0, 0
copy1, copy2, index, length = '', '', 0, -1
while counter < fraction_length:
copy1 += fraction[counter]
try:
index = fraction.index(copy1, counter + 1)
length = len(copy1)
copy2 = fraction[index: index + length]
if offset + length == index and length > 6:
break
except ValueError:
if index + 1 >= fraction_length:
return 'B'
offset += 1
copy1 = copy1[offset::]
counter += 1
return copy2
def problem26(limit):
fraction, longest = None, {'number': 0, 'length': 0}
recurring = None
one = Decimal(1)
for no in range(2, limit):
getcontext().prec = no * 2
fraction = one / Decimal(no)
recurring = get_recurring(fraction)
if longest['length'] >= len(recurring):
continue
longest['number'] = no
longest['length'] = len(recurring)
return longest
start_time = datetime.now()
number = problem26(1000)
endTime = datetime.now()
print('number: {}'.format(number))
runningTime = endTime - start_time
print('Running time (s): {}'.format(runningTime))