-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp151-200.py
205 lines (167 loc) · 5.12 KB
/
p151-200.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from helper import *
def problem_151(monte_carlo=False):
if monte_carlo:
simulations = 10**9
def cut_paper(cur_folder):
draw = random.sample(cur_folder, 1)[0]
cur_folder.remove(draw)
if draw == 1:
cur_folder.extend([2, 3, 4, 5])
elif draw == 2:
cur_folder.extend([3, 4, 5])
elif draw == 3:
cur_folder.extend([4, 5])
elif draw == 4:
cur_folder.append(5)
prob = 0
for i in range(1, simulations+1):
folder = [2, 3, 4, 5]
count = 0
for _ in range(14):
if len(folder) == 1:
count += 1
cut_paper(folder)
prob = prob + (count - prob)/i
return prob
else:
def evaluate(sheets):
num_sheets = sum(sheets)
single = 0
if num_sheets == 1 == sheets[-1]:
return 0
if num_sheets == 1 and sheets[0] == 0:
single = 1
for i in range(len(sheets)):
if sheets[i] == 0:
continue
nxt = sheets.copy()
nxt[i] -= 1
for j in range(i + 1, len(nxt)):
nxt[j] += 1
prob = sheets[i] / num_sheets
single += evaluate(nxt) * prob
return single
sheets = [1, 0, 0, 0, 0]
return evaluate(sheets)
def problem_162():
@Memoize
def count(digits, have_other=False, have_zero=False, have_one=False, have_a=False):
if have_zero and have_one and have_a and digits < 15:
return 16**digits
if digits == 0:
return 0
nxt = count(digits - 1, True, have_zero, have_one, have_a)
res = 13 * nxt
res += nxt if have_zero else count(digits-1, have_other, have_other, have_one, have_a)
res += nxt if have_one else count(digits-1, True, have_zero, True, have_a)
res += nxt if have_a else count(digits-1, True, have_zero, have_one, True)
return res
return hex(count(16)).upper()[2:]
def problem_164():
@Memoize
def create_num(a, b, size):
if size == 0:
return 1
tot = 0
for n in range(10):
if size == 20 and n == 0:
continue
if a + b + n > 9:
break
tot += create_num(b, n, size-1)
return tot
return create_num(0, 0, 20)
def problem_169(n=10**25, naive=False):
def naive_counting(lim):
def count_with_memory(num, pows, used):
if num == 0:
return 1
count = 0
for p in pows:
if used.count(p) >= 2:
continue
new_pows = tuple(x for x in pows if x <= p)
count += count_with_memory(num - p, new_pows, used + (p,))
return count
exp_bound = int(math.log(max(lim, 1), 2))
powers = tuple(2 ** x for x in range(exp_bound + 1))
return count_with_memory(lim, powers, ())
@Memoize
def count_ways(num):
if num == 0:
return 1
if num % 2 == 0:
return count_ways(num // 2) + count_ways((num - 2) // 2)
return count_ways((num - 1) // 2)
if naive:
return naive_counting(lim=n)
return count_ways(n)
def problem_179(lim=10**7):
arr = np.array([1]*lim)
for i in range(1, lim):
arr[i::i+1] += 1
return np.sum([x == 0 for x in np.diff(arr)])
def problem_183():
def D(N):
k = round(N / math.e)
while k % 2 == 0:
k /= 2
while k % 5 == 0:
k /= 5
if N % k == 0:
return -N
else:
return N
out = 0
for N in range(5, 10001):
out += D(N)
return out
def problem_187(lim=10**8):
sieve = primes_sieve(lim//2)
count = 0
lb = -1
for p in sieve:
lb += 1
ub = bisect.bisect_right(sieve, lim // p)
if lb > ub:
break
count += ub - lb
return count
def problem_188():
a = 1777
b = 1855
modulo = 10**8
return tetration(a, b, modulo)
def problem_191():
# This method works, but is much slower...
"""
global count
count = 0
def make_str(prev):
if len(prev) == 30:
global count
count += 1
return
for s in ['A', 'L', 'O']:
if s == 'L' and prev.count(s) > 0:
continue
if s == 'A' and prev[-2:].count(s) == 2:
continue
make_str(prev + [s])
make_str([])
"""
@Memoize
def count_str(absence, late, size):
if absence > 2 or late > 1:
return 0
if size == 0:
return 1
tot = 0
tot += count_str(absence=0, late=late, size=size-1)
tot += count_str(absence=absence+1, late=late, size=size-1)
tot += count_str(absence=0, late=late+1, size=size-1)
return tot
return count_str(0, 0, 30)
if __name__ == '__main__':
from pprint import pprint
pprint(problem_183())