## Mr. S. and Mr. P.

### October 23, 2009

John McCarthy, who discovered Lisp, attributes this puzzle to Hans Freudenthal:

We pick two numbers

aandb, so that 99 ≥a≥b≥ 2. We tell Mr. P. the producta×band Mr. S. the suma+b. Then Mr. S. and Mr. P. engage in the following dialog:Mr. P.: I don’t know the numbers.

Mr. S.: I knew you didn’t know. I don’t know either.

Mr. P.: Now I know the numbers.

Mr. S.: Now I know them too.

Find the numbers

aandb.

Your task is to find the two numbers. When you are finished, you are welcome to read or run a suggested solution, or to post your solution or discuss the exercise in the comments below.

Advertisements

Pages: 1 2

This puzzle is quite intriguing. There’s a Japanese website where users can post programming puzzles and solutions, and a variation of this puzzle was posted some time ago. You can browse solutions in whole bunch of different languages:

http://ja.doukaku.org/34/nested/ (descriptions are in japanese, but code is universal).

I like the solution using Python’s list comprehension. Haskell ones are also concise.

(Note: some posted solutions contained bugs and later corrected by comments).

The original puzzle posted in the above url limited the number of the range between 1 <= a <= b <= 13. Then upper bound became a variable N. As N gets larger there can be more than one solutions, and an interesting question was posted that whether you could tell the relationship between N and the number of solutions. Some guys checked solutions with varying N up to 1000 to find out the relation but couldn't. Do you know any reference that analyzes the pattern of solutions of this puzzle with varying N?

@Shiro: thanks for the link! A lot of gems are hidden away behind language barriers…

For those who are interested, I translated Oleg’s code into Clojure. It’s Lispy, it runs on the JVM, and best of all, it has lazy sequences a la Haskell.

http://github.com/hircus/clj-puzzles/blob/master/Mr-S-P.clj

@shiro intriguing indeed! Thanks for sharing the Japanese site — all too often, gems on the Internet lie undiscovered because of language barriers (even though the code itself is universal).

For those interested in Lispy solutions, I’ve translated Oleg’s code from Haskell to Clojure. It’s Lisp, it’s on the JVM, and best of all, it has lazy sequences. The translation is almost 1-to-1.

http://github.com/hircus/clj-puzzles/blob/master/Mr-S-P.clj

Cheers!

@shiro interesting indeed, thanks for sharing! While code is universal, sometimes language barrier means these gems don’t get found…

For those interested, I’ve translated Oleg’s Haskell code to Clojure. It’s Lisp, it’s on the JVM, and it has lazy sequences — the translation is virtually 1-to-1, apart from syntactic differences.

http://github.com/hircus/clj-puzzles/blob/master/Mr-S-P.clj

My solution is programmed in Erlang and can be viewed at PasteBin

The source code contains extensive explanation in comments.

Running my solution yields:

io:format(“Solution: ~w~n”, [pssolver:solve(2,99)]).

Solution: [{13,4}]

So the sought number pair is 13 and 4.

It was quite a challenge!

fact2 is irrelevant. why?

regards,

_raf

`Fact2?`

is used in the final calculation of`result`

. The calculation fails without`fact2?`

. What do you mean?fact2? is Mr S’s assertion that he doesn’t know: s=m+n is such that m and n can not be known (or, the list of good-summands for s has length > 1). The only “good-summand” in this sense is 4, but in that case both Mr P and Mr S know the numbers and we wouldn’t be having this pleasant conversation.

Here’s a slightly modified version of your program that produces ((13 4)) in plt-scheme-4.2.2: http://codepad.org/3BAukrSO

regards,

_raf

Correction: 4 *or 5* and “singleton? good-summands”…

Just ran my program a bit longer:

Once you increase N to around 850 you’ll have two solutions, not just 4 – 13.

The second solution is 4 – 61.

@Roy van Rijn – you should not get more solutions if you increase the limit. I’m guessing your program uses primes in some way, which is incorrect – it’s about unique factorisation given the limits of numbers, so its possible to uniquely factorise numbers into non-primes