Mr. S. and Mr. P.

October 23, 2009

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

We pick two numbers a and b, so that 99 ≥ ab ≥ 2. We tell Mr. P. the product a × b and Mr. S. the sum a + 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 a and b.

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.

About these ads

Pages: 1 2

11 Responses to “Mr. S. and Mr. P.”

  1. shiro said

    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?

  2. Michel said

    @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

  3. Michel said

    @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!

  4. Michel S. said

    @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

  5. Rudi Angela said

    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!

  6. _raf said

    fact2 is irrelevant. why?
    regards,
    _raf

  7. programmingpraxis said

    Fact2? is used in the final calculation of result. The calculation fails without fact2?. What do you mean?

  8. _raf said

    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

  9. _raf said

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

  10. 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.

  11. James Tipper said

    @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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 611 other followers

%d bloggers like this: