Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def getSum(x, y):
- #Returns the sum of x and y
- return x + y
- def getProduct(x, y):
- #Returns the product of x and y
- return x * y
- def getFactors(z):
- #Returns the factors of a positive integer z, but not itself and 1, and not if z is 2
- i = 2
- factorList = []
- while (i < z):
- if (z % i == 0):
- other = z / i
- if (i not in factorList and other not in factorList):
- factorList.append(i)
- factorList.append(z/i)
- i = i + 1
- return factorList
- def getAddends(z):
- #Returns the addends of a positive integer z, but not itself minus 1 and 1
- i = 2
- addendList = []
- while (i < (z-1)):
- if (i not in addendList):
- addendList.append(i)
- addendList.append(z-i)
- i = i + 1
- return addendList
- def hasOneFactorSumList(addendListSum):
- #Returns True if any addend pair's product has one factor, and false if they all have more than one
- i = 0
- oneFactor = False
- while (i < len(addendListSum)):
- firstNum = addendListSum[i]
- secondNum = addendListSum[i+1]
- hypotheticalProduct = getProduct(firstNum, secondNum)
- hypotheticalFactors = getFactors(hypotheticalProduct)
- if (len(hypotheticalFactors) <= 2):
- return True
- i = i + 2
- return False
- def findPossibleCombos(factorsListProduct):
- #Returns a possible combination for any factor pairs that don't sum to a number, that being a product has only one pair of factors
- possibleCombos = []
- i = 0
- while (i < len(factorsListProduct)):
- firstNum = factorsListProduct[i]
- secondNum = factorsListProduct[i+1]
- hypotheticalSum = getSum(firstNum, secondNum)
- #P is now mentally at what S would've seen before calculating to tell P that he knows P doesn't know
- hypotheticalAddends = getAddends(hypotheticalSum)
- oneFactor = hasOneFactorSumList(hypotheticalAddends)
- if (not oneFactor):
- possibleCombos.append(firstNum)
- possibleCombos.append(secondNum)
- i = i + 2
- return possibleCombos
- def riddleSolver():
- print("STARTING THE RIDDLE")
- for x in range(2,100):
- #reduce redundant checks where x and y are flipped
- for y in range(x,100):
- #x and y are different variables, can't be the same number
- if (x == y):
- continue
- print("COMPUTING FOR X: " + str(x) + " AND Y: " + str(y))
- productVar = getProduct(x,y)
- factorsListProduct = getFactors(productVar)
- #the product of x and y must have multiple factors or P guy would know right away
- if (len(factorsListProduct) >= 4 and len(factorsListProduct) % 2 == 0):
- sumVar = getSum(x,y)
- addendListSum = getAddends(sumVar)
- #the sum of x and y must have multiple addends or S guy would know right away
- if (len(addendListSum) >= 4 and len(addendListSum) % 2 == 0):
- #none of those addend pairs must produce a number that being a product has only one pair of factors or else S can't tell P he doesn't know
- oneFactor = hasOneFactorSumList(addendListSum)
- if (not oneFactor):
- #P can now sum his original factors list and find which pair(s) do(es)n't sum to a number that would let S tell him that, namely, which pair doesn't sum to a number that being a product has only one pair of factors
- possiblePCombos = findPossibleCombos(factorsListProduct)
- if (len(possiblePCombos) == 2):
- #P now knows what x and y are and tells S that he does, which spurs S to also know, this means that P had eliminated all other factor pairs because they sum to a number that being a product has only one pair of factors which wouldnt've allowed S to make his statement
- print(str(possiblePCombos) + " IS A POSSIBLE COMBINATION")
- #S can do the same math with all of his addend pairs, getting a product for each, and then getting factors pairs for those, and finding the one which doesn't sum to a number that being a product has only one pair of factors
- #Only one of S's addend pairs must meet this criteria
- criteriaCounter = 0
- solution = []
- i = 0
- while (i < len(addendListSum)):
- firstNum = addendListSum[i]
- secondNum = addendListSum[i+1]
- sumProduct = getProduct(firstNum,secondNum)
- sumProductFactors = getFactors(sumProduct)
- possibleSCombos = findPossibleCombos(sumProductFactors)
- if (len(possibleSCombos) == 2):
- criteriaCounter = criteriaCounter + 1
- solution = possibleSCombos
- if (criteriaCounter > 1):
- break
- i = i + 2
- if (criteriaCounter == 1):
- print("SOLVED! " + str(solution) + " IS THE ANSWER")
- return
- else:
- continue
- else:
- continue
- else:
- continue
- else:
- continue
- print("RIDDLE COMPLETED WITHOUT FINDING AN ANSWER")
- riddleSolver()
Add Comment
Please, Sign In to add comment