How to resolve the algorithm Sum and product puzzle step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sum and product puzzle step by step in the 11l programming language

Table of Contents

Problem Statement

Solve the "Impossible Puzzle": It can be hard to wrap one's head around what the three lines of dialog between S (the "sum guy") and P (the "product guy") convey about the values of X and Y. So for your convenience, here's a break-down: Terminology:

Your program can solve the puzzle by considering all possible pairs (X, Y) in the range 2 ≤ X < Y ≤ 98, and then successively eliminating candidates based on the three facts. It turns out only one solution remains! See the Python example for an implementation that uses this approach with a few optimizations.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sum and product puzzle step by step in the 11l programming language

Source code in the 11l programming language

F counter(arr)
   DefaultDict[Int, Int] d
   L(a) arr
      d[a]++
   R d

F decompose_sum(s)
   R (2 .< Int(s / 2 + 1)).map(a -> (a, @s - a))

Set[(Int, Int)] all_pairs_set
L(a) 2..99
   L(b) a + 1 .< 100
      I a + b < 100
         all_pairs_set.add((a, b))
V all_pairs = Array(all_pairs_set)

V product_counts = counter(all_pairs.map((c, d) -> c * d))
V unique_products = Set(all_pairs.filter((a, b) -> :product_counts[a * b] == 1))
V s_pairs = all_pairs.filter((a, b) -> all(decompose_sum(a + b).map((x, y) -> (x, y) !C :unique_products)))

product_counts = counter(s_pairs.map((c, d) -> c * d))
V p_pairs = s_pairs.filter((a, b) -> :product_counts[a * b] == 1)

V sum_counts = counter(p_pairs.map((c, d) -> c + d))
V final_pairs = p_pairs.filter((a, b) -> :sum_counts[a + b] == 1)

print(final_pairs)

  

You may also check:How to resolve the algorithm Square-free integers step by step in the Pascal programming language
You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the BASIC programming language
You may also check:How to resolve the algorithm Factorial step by step in the RASEL programming language
You may also check:How to resolve the algorithm Maze generation step by step in the C++ programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the ML/I programming language