-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumProductComputer.py
More file actions
411 lines (326 loc) · 17.8 KB
/
SumProductComputer.py
File metadata and controls
411 lines (326 loc) · 17.8 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
class SumProductComputer:
@staticmethod
def print_generic_problem_description():
print('----- Description -----')
print('Checks every pair of (not equal) numbers, who are at least than arg_min (default 2, first parameter) '
'and whose sum is not greater than than sum_max (default 100, second parameter).')
print('Checks those pairs for the truth of these four statements:')
print(' 1: Someone who knows the product does not know what the numbers are.')
print(' 2: Someone who knows sum knows that statement #1 must be the case.')
print(' 3: Someone who knows statements #1-2 and knows the product DOES know what the numbers are.')
print(' 4: Someone who knows statements #1-3 and knows the sum DOES know what the numbers are.')
print('Visit https://en.wikipedia.org/wiki/Sum_and_Product_Puzzle for details')
def __init__(self, arg_min=2, sum_max=100, verbosity=3):
self.arg_min = arg_min
self.sum_max = sum_max
self.possible_pairs = []
# verbosity uses integer levels.
# 1 = don't print anything
# 2 = print everything
# 3 and up = print the first n-2 levels.
self.verbosity = self.set_verbosity(verbosity)
self.depth = 0
def compute_possible_pairs(self):
self.depth = 0
self.print_if('----- Checking Possibilities -----')
for x in range(self.arg_min, self.largest_x()):
for y in range(x, self.sum_max):
if self.satisfies_conditions(x, y):
self.print_if('{0} and {1} satisfy the conditions!'.format(x, y))
self.possible_pairs.append((x, y))
self.print_if('----- Finished -----')
self.print_if('')
return self.possible_pairs
def print_problem_description(self):
print('----- Description -----')
print('Checks every pair of (not equal) numbers, who are at least than arg_min ({0}) '
'and whose sum is not greater than sum_max ({1}).'.format(str(self.arg_min), str(self.sum_max)))
print('Checks those pairs for the truth of these four statements:')
print(' 1: Someone who knows the product does not know what the numbers are.')
print(' 2: Someone who knows sum knows that statement #1 must be the case.')
print(' 3: Someone who knows statements #1-2 and knows the product DOES know what the numbers are.')
print(' 4: Someone who knows statements #1-3 and knows the sum DOES know what the numbers are.')
print('Visit https://en.wikipedia.org/wiki/Sum_and_Product_Puzzle for details')
print('')
def print_parameters(self):
print('----- Parameters -----')
print('Y > X >= ' + str(self.arg_min))
print('X + Y <= ' + str(self.sum_max))
print('')
def satisfies_conditions(self, x, y):
if not self.satisfies_basic_conditions(x, y):
return False
if not self.satisfies_statements(x, y):
return False
return True
def satisfies_statements(self, x, y):
self.depth += 1
satisfies_first = self.satisfies_first_statement(x, y)
self.depth -= 1
if not satisfies_first:
self.print_if('{0} and {1} doesn\'t work. Someone who knows the product will '
'know what they are.'.format(x, y))
return False
self.depth += 1
satisfies_second = self.satisfies_second_statement(x, y)
self.depth -= 1
if not satisfies_second:
self.print_if('{0} and {1} doesn\'t work. Someone who knows the sum would not know that the product'
' does not discern the numbers'.format(x, y))
return False
self.depth += 1
satisfies_third = self.satisfies_third_statement(x, y)
self.depth -= 1
if not satisfies_third:
self.print_if('{0} and {1} doesn\'t work. Someone who knows the product and statements #1 and #2'
' will not know what the numbers are.'.format(x, y))
return False
self.depth += 1
satisfies_fourth = self.satisfies_fourth_statement(x, y)
self.depth -= 1
if not satisfies_fourth:
self.print_if('{0} and {1} doesn\'t work. Someone who knows the sum and statements #1, #2 and #3'
' will not know what the numbers are.'.format(x, y))
return False
return True
def satisfies_basic_conditions(self, x, y):
if self.x_gte_y(x, y):
self.print_if('{0} and {1} doesn\'t work. x ({0}) is greater than '
'or equal to y ({1}).'.format(x, y))
return False
if self.arg_too_small(x):
self.print_if('{0} and {1} doesn\'t work. {0} is not greater than {2}.'.format(x, y, self.arg_min))
return False
if self.sum_too_large(x, y):
self.print_if('{0} and {1} doesn\'t work. Sum is greater than {2}.'.format(x, y, self.sum_max))
return False
return True
# 1. Someone who knows their product will know what they are.
def satisfies_first_statement(self, x, y):
product = x * y
self.depth += 1
pairs = self.get_satisfactory_pairs_with_product(product, 2)
self.depth -= 1
if len(pairs) < 2:
self.print_if('{0} and {1} doesn\'t satisfy #1. There does not exist two pairs with'
' their product ({2})'.format(x, y, product))
return False
self.print_if('{0} and {1} satisfy #1! Someone who knows their product will not know what they are,'
' since ({2},{3}) and ({4},{5}) have the same '
'product.'.format(x, y, pairs[0][0], pairs[0][1], pairs[1][0], pairs[1][1]))
return True
def satisfies_second_statement(self, x, y):
sum = x + y
possible_pairs = []
self.depth += 1
ns_that_dont_satisfy_first = self.get_pair_that_doesnt_satisfy_first(sum)
self.depth -= 1
if ns_that_dont_satisfy_first is not None:
self.print_if('{0} and {1} doesn\'t satisfy #2. The sum ({2}) does not indicate that the product is not '
'enough to discern x and y. ({3},{4}) is a possible alternative that sums to {2} but whose '
'product would give them away.'
.format(x, y, sum, ns_that_dont_satisfy_first[0], ns_that_dont_satisfy_first[1]))
return False
self.print_if('{0} and {1} satisfies #2! The sum ({2}) is enough to know the product '
'does not discern the x and y'.format(x, y, sum))
return True
def satisfies_third_statement(self, x, y):
product = x * y
self.depth += 1
pairs = self.get_satisfactory_pairs_3(product, 2)
self.depth -= 1
if len(pairs) > 1:
self.print_if('{0} and {1} doesn\'t satisfy #3. Someone who knows their product cannot figure them'
' out from #1 and #2, since both ({2},{3}) and ({4},{5}) are options.'
.format(x, y, pairs[0][0], pairs[0][1], pairs[1][0], pairs[1][1]))
return False
self.print_if('{0} and {1} satisfy #3! Someone who knows their product can discern'
' them from #1 and #2.'.format(x, y))
return True
def satisfies_fourth_statement(self, x, y):
sum = x + y
self.depth += 1
pairs = self.get_satisfactory_pairs_4(sum, 2)
self.depth -= 1
if len(pairs) > 1:
self.print_if('{0} and {1} doesn\'t satisfy #4. Someone who knows their sum cannot figure them'
' out from #1, #2 and #3, since both ({2},{3}) and ({4},{5}) are options.'
.format(x, y, pairs[0][0], pairs[0][1], pairs[1][0], pairs[1][1]))
return False
self.print_if('{0} and {1} satisfy #4! Someone who knows their sum can discern'
' them from #1, #2 and #3.'.format(x, y))
return True
def x_gte_y(self, x, y):
return x >= y
def arg_too_small(self, x):
return x < self.arg_min
def sum_too_large(self, x, y):
return x + y > self.sum_max
def get_satisfactory_pairs_with_product(self, target_product, max_pairs_to_find):
satisfactory_pairs = []
for possible_x in range(self.arg_min, min(target_product, self.largest_x())):
if target_product % possible_x != 0:
self.print_if('{0} doesn\'t work. It cannot be part of a satisfactory pair with product {1}.'
' It is not a factor of {1}.'.format(possible_x, target_product))
continue
possible_y = target_product / possible_x
self.depth += 1
satisfies_basic = self.satisfies_basic_conditions(possible_x, possible_y)
self.depth -= 1
if satisfies_basic:
self.print_if('{0} and {1} work! They can be used to create product {2}'
.format(possible_x, possible_y, target_product))
satisfactory_pairs.append((possible_x, possible_y))
if len(satisfactory_pairs) >= max_pairs_to_find:
break
else:
self.print_if('{0} and {1} doesn\'t work. They cannot be used to create product {2}.'
' They do not satisfy the basic conditions'
.format(possible_x, possible_y, target_product))
return satisfactory_pairs
def get_pair_that_doesnt_satisfy_first(self, target_sum):
for possible_x in range(self.arg_min, target_sum):
possible_y = target_sum - possible_x
self.depth += 1
satisfies_basic = self.satisfies_basic_conditions(possible_x, possible_y)
self.depth -= 1
if satisfies_basic:
self.depth += 1
satisfies_first = self.satisfies_first_statement(possible_x, possible_y)
self.depth -= 1
if not satisfies_first:
self.print_if('{0} and {1} are discernable from the product.'.format(possible_x, possible_y))
return (possible_x, possible_y)
else:
self.print_if('{0} and {1} aren\'t discernable from the product.'.format(possible_x, possible_y))
return None
def get_satisfactory_pairs_with_sum(self, target_sum, max_pairs_to_find):
satisfactory_pairs = []
for possible_x in range(self.arg_min, target_sum):
possible_y = target_sum - possible_x
self.depth += 1
satisfies_basic_conditions = self.satisfies_basic_conditions(possible_x, possible_y)
self.depth -= 1
if satisfies_basic_conditions:
self.depth += 1
satisfies_first = self.satisfies_first_statement(possible_x, possible_y)
self.depth -= 1
if satisfies_first:
self.print_if('{0} and {1} works! They add up to {2} and their product ({3})'
' does not discern their identity.'
.format(possible_x, possible_y, target_sum, possible_x*possible_y))
satisfactory_pairs.append((possible_x, possible_y))
if len(satisfactory_pairs) >= max_pairs_to_find:
break
else:
self.print_if('{0} and {1} doesn\'t work. Their product ({2}) discerns their identity.'
.format(possible_x, possible_y, possible_x*possible_y))
else:
self.print_if('{0} and {1} doesn\'t work. They cannot be used to create sum {2} since'
' they do not satisfy the basic conditions.'.format(possible_x, possible_y, target_sum))
return satisfactory_pairs
def get_satisfactory_pairs_3(self, target_product, max_pairs_to_find):
satisfactory_pairs = []
for possible_x in range(self.arg_min, min(target_product, self.largest_x())):
if target_product % possible_x != 0:
self.print_if('{0} doesn\'t work. It cannot be part of a satisfactory pair with product {1}.'
' It is not a factor of {1}.'.format(possible_x, target_product))
continue
possible_y = target_product / possible_x
self.depth += 1
satisfies_basic = self.satisfies_basic_conditions(possible_x, possible_y)
self.depth -= 1
if satisfies_basic:
self.depth += 1
satisfies_first = self.satisfies_first_statement(possible_x, possible_y)
self.depth -= 1
if satisfies_first:
self.depth += 1
satisfies_second = self.satisfies_second_statement(possible_x, possible_y)
self.depth -= 1
if satisfies_second:
self.print_if('{0} and {1} works! Their product is {2} and does not give them away, and the'
' sum is enough to know that.'.format(possible_x, possible_y, target_product))
satisfactory_pairs.append((possible_x, possible_y))
if len(satisfactory_pairs) >= max_pairs_to_find:
break
else:
self.print_if('{0} and {1} doesn\'t work. Their sum is not enough to know that the'
' product does not give them away.'.format(possible_x, possible_y))
else:
self.print_if('{0} and {1} doesn\'t work. They are immidiately discernable by their product.'
.format(possible_x, possible_y))
else:
self.print_if('{0} and {1} doesn\'t work. They cannot be used to create product {2}.'
' They do not satisfy the basic conditions'
.format(possible_x, possible_y, target_product))
return satisfactory_pairs
def get_satisfactory_pairs_4(self, target_sum, max_pairs_to_find):
satisfactory_pairs = []
for possible_x in range(self.arg_min, min(target_sum, self.largest_x())):
possible_y = target_sum - possible_x
self.depth += 1
satisfies_basic = self.satisfies_basic_conditions(possible_x, possible_y)
self.depth -= 1
if satisfies_basic:
self.depth += 1
satisfies_first = self.satisfies_first_statement(possible_x, possible_y)
self.depth -= 1
if satisfies_first:
self.depth += 1
satisfies_second = self.satisfies_second_statement(possible_x, possible_y)
self.depth -= 1
if satisfies_second:
self.depth += 1
satisfies_third = self.satisfies_third_statement(possible_x, possible_y)
self.depth -= 1
if satisfies_third:
self.print_if('{0} and {1} works! Their sum is {2} and is enough to know that the'
' product does not give them away. As well, all other factors do not'
' imply the products un-guessability.'
.format(possible_x, possible_y, target_sum))
satisfactory_pairs.append((possible_x, possible_y))
if len(satisfactory_pairs) >= max_pairs_to_find:
break
else:
self.print_if('{0} and {1} doesn\'t work. Other factors of their product imply the'
' products un-guessability'.format(possible_x, possible_y))
else:
self.print_if('{0} and {1} doesn\'t work. Their sum is not enough to know that the'
' product does not give them away.'.format(possible_x, possible_y))
else:
self.print_if('{0} and {1} doesn\'t work. They are immidiately discernable by their product.'
.format(possible_x, possible_y))
else:
self.print_if('{0} and {1} doesn\'t work. They cannot be used to create sum {2}.'
' They do not satisfy the basic conditions'
.format(possible_x, possible_y, target_sum))
return satisfactory_pairs
def largest_x(self):
if self.sum_max % 2 == 0:
return (self.sum_max / 2) - 1
return (self.sum_max - 1) / 2
# level is the depth of this print. the printed string will be indented that many times
# additionally, a level of 0 is considered "high-level" and is printed in verbosity=2 situations. Nothing else is.
def print_if(self, text):
indent = ' '
if self.verbosity == 1:
return
if self.verbosity == 2:
print('{0}{1}'.format(indent * self.depth, text))
return
if self.verbosity > 2:
if self.depth < self.verbosity-2:
print('{0}{1}'.format(indent * self.depth, text))
return
else:
return
raise Exception('invalid verbosity level {0}'.format(self.verbosity))
@staticmethod
def set_verbosity(verbosity):
if verbosity < 0:
raise Exception('Invalid verbosity {0}. Must be above 0. 0=default, 1=nothing, 2=everything,'
' 3+ = n-1 levels of text'.format(verbosity))
if verbosity == 0:
return 3
return verbosity