Created
October 25, 2013 18:15
-
-
Save vishvananda/7159301 to your computer and use it in GitHub Desktop.
Solution for the sum product puzzle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
answers = set() | |
MAX = 100 | |
for a in xrange(2, MAX): | |
for b in xrange(a, MAX): | |
answers.add((a, b)) | |
print len(answers) | |
# sum knows product doesn't know the answer so potential a, b can't have one product solution | |
products = {} | |
sums = {} | |
for a, b in answers: | |
sums.setdefault(a + b, set()) | |
sums[a + b].add((a, b)) | |
products.setdefault(a * b, set()) | |
products[a * b].add((a, b)) | |
potential_sums = set() | |
for (s, values) in sums.iteritems(): | |
allowed = True | |
for a, b in values: | |
if len(products.get(a * b, [])) == 1: | |
allowed = False | |
if allowed: | |
potential_sums.add(s) | |
answers = filter(lambda (a, b): a + b in potential_sums, answers) | |
print len(answers) | |
# of the remaining solutions if there is only one product then product would know the answer | |
# since product knows the answer then the solution must have == 1 product | |
products = {} | |
for a, b in answers: | |
products.setdefault(a * b, set()) | |
products[a * b].add((a, b)) | |
answers = filter(lambda (a, b): len(products.get(a * b, [])) == 1, answers) | |
print len(answers) | |
# of the remaining solutions if there is only one sum then sum would know the answer | |
# since sum does know the answer then the solution must have == 1 sum | |
sums = {} | |
for a, b in answers: | |
sums.setdefault(a + b, set()) | |
sums[a + b].add((a, b)) | |
answers = filter(lambda (a, b): len(sums.get(a + b, [])) == 1, answers) | |
print len(answers) | |
print answers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment