Sum and product puzzle: Difference between revisions
Content added Content deleted
m (→version 2: corrected a misspelling.) |
Alextretyak (talk | contribs) (Added 11l) |
||
Line 51: | Line 51: | ||
* Wikipedia: [[wp:Sum and Product Puzzle|Sum and Product Puzzle]] |
* Wikipedia: [[wp:Sum and Product Puzzle|Sum and Product Puzzle]] |
||
<hr> |
<hr> |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<lang 11l>F counter(arr) |
|||
V d = DefaultDict[Int, Int]() |
|||
L(a) arr |
|||
d[a]++ |
|||
R d |
|||
F decompose_sum(s) |
|||
R (2 .< Int(s / 2 + 1)).map(a -> (a, @s - a)) |
|||
V all_pairs_set = Set[(Int, Int)]() |
|||
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)</lang> |
|||
{{out}} |
|||
<pre> |
|||
[(4, 13)] |
|||
</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |