-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26.hs
50 lines (44 loc) · 1.68 KB
/
26.hs
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
def div (num, denom, CallsLeft = 100):
assert(num < denom)
def recursiveDivision(num, denom, listOfDenoms, CallsLeft):
try:
indexOfThing = listOfDenoms.index((num%denom)*10)
return len(listOfDenoms)-indexOfThing
except:
if (CallsLeft == 0):
return -1
else:
if (num%denom == 0):
return 0
else:
return recursiveDivision((num%denom)*10, denom, listOfDenoms+[(num%denom)*10], CallsLeft-1)
return recursiveDivision(num*10, denom, [], CallsLeft)
maxRepeatDepth = 0
indexOfMaxRepeat = -1
for index in range(2, 1000):
indexRepeat = div(1, index)
if (indexRepeat == -1):
indexRepeat = div(1, index, 1000)
if (indexRepeat > maxRepeatDepth):
maxRepeatDepth = indexRepeat
indexOfMaxRepeat = index
print(maxRepeatDepth)
print(indexOfMaxRepeat)
"""
manual division
def div (num, denom, CallsLeft = 100):
assert(num < denom)
def recursiveDivision(num, denom, listOfDenoms, CallsLeft):
try:
indexOfThing = listOfDenoms.index((num%denom)*10)
return str(num//denom)+" repeated length is "+ str(len(listOfDenoms)-indexOfThing)
except:
if (CallsLeft == 0):
return " ran out of calls. fraction probably doesn't repeat"
else:
if (num%denom == 0):
return str(num//denom)+" fraction doesn't repeat"
else:
return str(num//denom)+recursiveDivision((num%denom)*10, denom, listOfDenoms+[(num%denom)*10], CallsLeft-1)
return "."+recursiveDivision(num*10, denom, [], CallsLeft)
"""