Skip to content

Instantly share code, notes, and snippets.

@joaospinto
Created August 2, 2019 13:10
Show Gist options
  • Save joaospinto/7d835301bc873ee6f5d7d96fa1644706 to your computer and use it in GitHub Desktop.
Save joaospinto/7d835301bc873ee6f5d7d96fa1644706 to your computer and use it in GitHub Desktop.
Solves a problem about guessing numbers from sums and products
products = dict()
for i in range(2, 101):
for j in range(i, 101):
products[i * j] = products.get(i * j, []) + [(i, j)]
ambiguous_products = set()
for product in products:
if len(products[product]) > 1:
ambiguous_products.add(product)
possible_sums = []
for s in range(4, 201):
all_ambiguous = True
for i in range(2, s // 2 + 1):
j = s - i
product = i * j
if product not in ambiguous_products:
all_ambiguous = False
break
if all_ambiguous:
possible_sums.append(s)
new_products = dict()
for s in possible_sums:
for i in range(2, s // 2 + 1):
j = s - i
new_products[i * j] = new_products.get(i * j, []) + [(i, j)]
unique_products = set()
for product in new_products:
if len(new_products[product]) == 1:
unique_products.add(product)
sums = dict()
for product in unique_products:
for i, j in new_products[product]:
sums[i + j] = sums.get(i + j, []) + [(i, j)]
unique_sums = []
for s in sums:
if len(sums[s]) == 1:
unique_sums.append(s)
assert(len(unique_sums) == 1)
real_sum = unique_sums[0]
candidates = []
for i in range(2, real_sum // 2 + 1):
j = real_sum - i
if i * j in unique_products:
candidates.append((i, j))
assert(len(candidates) == 1)
print(candidates[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment