Guest User

Untitled

a guest
Sep 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. def getSum(x, y):
  2. #Returns the sum of x and y
  3. return x + y
  4.  
  5. def getProduct(x, y):
  6. #Returns the product of x and y
  7. return x * y
  8.  
  9. def getFactors(z):
  10. #Returns the factors of a positive integer z, but not itself and 1, and not if z is 2
  11. i = 2
  12. factorList = []
  13. while (i < z):
  14. if (z % i == 0):
  15. other = z / i
  16. if (i not in factorList and other not in factorList):
  17. factorList.append(i)
  18. factorList.append(z/i)
  19. i = i + 1
  20. return factorList
  21.  
  22. def getAddends(z):
  23. #Returns the addends of a positive integer z, but not itself minus 1 and 1
  24. i = 2
  25. addendList = []
  26. while (i < (z-1)):
  27. if (i not in addendList):
  28. addendList.append(i)
  29. addendList.append(z-i)
  30. i = i + 1
  31. return addendList
  32.  
  33. def hasOneFactorSumList(addendListSum):
  34. #Returns True if any addend pair's product has one factor, and false if they all have more than one
  35. i = 0
  36. oneFactor = False
  37. while (i < len(addendListSum)):
  38. firstNum = addendListSum[i]
  39. secondNum = addendListSum[i+1]
  40. hypotheticalProduct = getProduct(firstNum, secondNum)
  41. hypotheticalFactors = getFactors(hypotheticalProduct)
  42. if (len(hypotheticalFactors) <= 2):
  43. return True
  44. i = i + 2
  45. return False
  46.  
  47. def findPossibleCombos(factorsListProduct):
  48. #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
  49. possibleCombos = []
  50. i = 0
  51. while (i < len(factorsListProduct)):
  52. firstNum = factorsListProduct[i]
  53. secondNum = factorsListProduct[i+1]
  54. hypotheticalSum = getSum(firstNum, secondNum)
  55. #P is now mentally at what S would've seen before calculating to tell P that he knows P doesn't know
  56. hypotheticalAddends = getAddends(hypotheticalSum)
  57. oneFactor = hasOneFactorSumList(hypotheticalAddends)
  58. if (not oneFactor):
  59. possibleCombos.append(firstNum)
  60. possibleCombos.append(secondNum)
  61. i = i + 2
  62. return possibleCombos
  63.  
  64. def riddleSolver():
  65. print("STARTING THE RIDDLE")
  66. for x in range(2,100):
  67. #reduce redundant checks where x and y are flipped
  68. for y in range(x,100):
  69. #x and y are different variables, can't be the same number
  70. if (x == y):
  71. continue
  72. print("COMPUTING FOR X: " + str(x) + " AND Y: " + str(y))
  73. productVar = getProduct(x,y)
  74. factorsListProduct = getFactors(productVar)
  75. #the product of x and y must have multiple factors or P guy would know right away
  76. if (len(factorsListProduct) >= 4 and len(factorsListProduct) % 2 == 0):
  77. sumVar = getSum(x,y)
  78. addendListSum = getAddends(sumVar)
  79. #the sum of x and y must have multiple addends or S guy would know right away
  80. if (len(addendListSum) >= 4 and len(addendListSum) % 2 == 0):
  81. #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
  82. oneFactor = hasOneFactorSumList(addendListSum)
  83. if (not oneFactor):
  84. #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
  85. possiblePCombos = findPossibleCombos(factorsListProduct)
  86. if (len(possiblePCombos) == 2):
  87. #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
  88. print(str(possiblePCombos) + " IS A POSSIBLE COMBINATION")
  89. #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
  90. #Only one of S's addend pairs must meet this criteria
  91. criteriaCounter = 0
  92. solution = []
  93. i = 0
  94. while (i < len(addendListSum)):
  95. firstNum = addendListSum[i]
  96. secondNum = addendListSum[i+1]
  97. sumProduct = getProduct(firstNum,secondNum)
  98. sumProductFactors = getFactors(sumProduct)
  99. possibleSCombos = findPossibleCombos(sumProductFactors)
  100. if (len(possibleSCombos) == 2):
  101. criteriaCounter = criteriaCounter + 1
  102. solution = possibleSCombos
  103. if (criteriaCounter > 1):
  104. break
  105. i = i + 2
  106. if (criteriaCounter == 1):
  107. print("SOLVED! " + str(solution) + " IS THE ANSWER")
  108. return
  109. else:
  110. continue
  111. else:
  112. continue
  113. else:
  114. continue
  115. else:
  116. continue
  117. print("RIDDLE COMPLETED WITHOUT FINDING AN ANSWER")
  118.  
  119. riddleSolver()
Add Comment
Please, Sign In to add comment