# Product and Sum, Variant 2b

My friends Peter and Sam are excellent mathematicians and always think strictly logical. Yesterday I told them: "I have secretly chosen two integers $x$ and $y$ with $1\le x\le y\le 99$. I have told their sum $s=x+y$ to Sam and their product $p=xy$ to Peter." Then the following conversation developed.

1. Peter said: I don't know the numbers.
2. Sam said: I don't know the numbers.
3. Peter said: I don't know the numbers.
4. Sam said: I don't know the numbers.
5. Peter said: I don't know the numbers.
6. Sam said: I don't know the numbers.
7. Peter said: I don't know the numbers.
8. Sam said: I don't know the numbers.
9. Peter said: I don't know the numbers.
10. Sam said: I don't know the numbers.
11. Peter said: I don't know the numbers.
12. Sam said: I don't know the numbers.
13. Peter said: I don't know the numbers.
14. Sam said: I don't know the numbers.
15. Peter said: Aha. Then I do know the numbers now.
16. Sam said: Aha. Then I also know the numbers now.

What are the values of $x$ and $y$?

Bonus question: How does the answer change, if we replace the lower bound in "$1\le x\le y\le 99$" by the new lower bound "$66\le x\le y\le 99$"

Remark: This puzzle is a variation on Product and Sum, Variant 2a.

• I would be curious to see a generalized version of this puzzle... – Aza Feb 1 '15 at 18:58

I wrote a Ruby program to cut out the tedious manual work:

#!/usr/bin/ruby

range = [*1..99]

pairs = range.product(range).select{|x, y| x <= y }
pairs.map! {|x, y| [x, y, x+y, x*y] }

def getuniqs arr
arr.group_by{|x| x }
.map{|k, v| v.length == 1 ? k : nil }
.compact
end

def dunno pairs, n
uniqs = getuniqs pairs.map{|p| p[n] }
pairs.reject{|pair| uniqs.index pair[n] }
end

def aha pairs, n
uniqs = getuniqs pairs.map{|p| p[n] }
pairs.select{|pair| uniqs.index pair[n] }
end

7.times {
pairs = dunno pairs, 3  # peter
pairs = dunno pairs, 2  # sam
}
pairs = aha pairs, 3

puts pairs.map{|x, y, s, p| "#{x}, #{y}" }


Results:

llama@llama:...Code/ruby/puzzling8440$./pro_sum.rb 77, 84  The numbers are$77$and$84\$.

For the bonus: The results are exactly the same.

This uses the same strategy as the answer on Variant 2A.

Scala solution:

https://ideone.com/X48DIB

case class State(a: Int, b: Int, sum:Int, prod:Int)

val allPossibleStates:Seq[State] = for {
i <- 1 to 99;
j <- i to 99
} yield State(i,j,i+j,i*j)

(1 to 7).foldLeft(allPossibleStates)( (states, _) =>
states
.groupBy(_.prod).filter(_._2.size > 1).flatMap(_._2)
.groupBy(_.sum).filter(_._2.size > 1).flatMap(_._2)
.toSeq
).groupBy(_.prod).filter(_._2.size == 1).flatMap(_._2)
.map(println)