I am often asked β€œwhat is APL good for”? I reply that APL is good for almost anything but that it is also very good at prototyping. With it you can experiment and use it as a tool for thinking about the problem at hand. It is easy in APL to manipulate data and build tools to get a better view of the problem and come up with solutions. In the following text we will use APL to think of a solution to a problem involving calculations to solve a mathematical problem. The problem originates from Kakuro[1], a popular puzzle found in newspapers, where you need to know the sets of numbers making up a solution.

The problem

In this problem we need to come up with all the sets of N unique positive single digit numbers (1..9) making up a particular sum S. N and S are the key numbers here, they will be the input to our problem. The output is all the possible sets. The format is unimportant; it could be e.g. a list of sets or a square matrix, N wide.

Most of the code should work in any modern APL. However, the examples were created with Dyalog Version 14. When features are used which are available in Dyalog APL only this is mentioned. βŽ•IO←1 is assumed.

For example, there are only 2 sets of 4 unique digits 1 to 9 adding up to 12: (1 2 3 6) and (1 2 4 5).

Attempt #1

The first thought is the easiest: brute force. Can we generate all the possibilities and screen out unwanted ones?

We need to form sequences of N numbers, each from 1 to 9. For example, pairs are (1,1) (1,2) (1,3)…(2,1) (2,2)… (9,9), 81 combinations in all. We can use catenate (,) to put numbers together:

      i3 ← ⍳3  ⍝ define a vector of the numbers 1, 2 and 3
      i3 , i3
1 2 3 1 2 3

Not quite what we want, we want to catenate each number to each other:

      i3 ,Β¨ i3
 1 1  2 2  3 3

In Dyalog APL V14 there is a new user command that allows us to box enclosed arrays automatically to better see their nature:

      ]box on
Was OFF
      i3 ,Β¨ i3
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 1β”‚2 2β”‚3 3β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

Again this is not what we want, what we want is to do the catenation for each element in i3 to each other element in i3, like this:

      1 ,Β¨ i3
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 1β”‚1 2β”‚1 3β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
      2 ,Β¨ i3
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚2 1β”‚2 2β”‚2 3β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
      3 ,Β¨ i3
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚3 1β”‚3 2β”‚3 3β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

APL allows us to do this nicely, distributing the function , without looping, using jot-dot (∘.) :

     i3 ∘., i3
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 1β”‚1 2β”‚1 3β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚2 1β”‚2 2β”‚2 3β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚3 1β”‚3 2β”‚3 3β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

That’s better. This will work is all modern APLs. In Dyalog we can use commute (⍨) to avoid repeating the argument. Commute normally swaps (commutes) the arguments of a function so a∊⍨b becomes b∊a, but when used monadically it repeats the argument so +⍨a becomes a+a:

      ∘., ⍨ i3
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 1β”‚1 2β”‚1 3β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚2 1β”‚2 2β”‚2 3β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚3 1β”‚3 2β”‚3 3β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

Let’s do it for the numbers 1 to 9:

      ∘., ⍨ ⍳9
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 1β”‚1 2β”‚1 3β”‚1 4β”‚1 5β”‚1 6β”‚1 7β”‚1 8β”‚1 9β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚2 1β”‚2 2β”‚2 3β”‚2 4β”‚2 5β”‚2 6β”‚2 7β”‚2 8β”‚2 9β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚3 1β”‚3 2β”‚3 3β”‚3 4β”‚3 5β”‚3 6β”‚3 7β”‚3 8β”‚3 9β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚4 1β”‚4 2β”‚4 3β”‚4 4β”‚4 5β”‚4 6β”‚4 7β”‚4 8β”‚4 9β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚5 1β”‚5 2β”‚5 3β”‚5 4β”‚5 5β”‚5 6β”‚5 7β”‚5 8β”‚5 9β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚6 1β”‚6 2β”‚6 3β”‚6 4β”‚6 5β”‚6 6β”‚6 7β”‚6 8β”‚6 9β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚7 1β”‚7 2β”‚7 3β”‚7 4β”‚7 5β”‚7 6β”‚7 7β”‚7 8β”‚7 9β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚8 1β”‚8 2β”‚8 3β”‚8 4β”‚8 5β”‚8 6β”‚8 7β”‚8 8β”‚8 9β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚9 1β”‚9 2β”‚9 3β”‚9 4β”‚9 5β”‚9 6β”‚9 7β”‚9 8β”‚9 9β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

Let’s keep this list in a variable and find the sum of each and let’s find those that add up to, say, 6:

      v← , ∘.,⍨ ⍳9  ⍝ turn the table into a list with ravel (,)
      sum← +/Β¨ v    ⍝ sum each set
      six← 6=sum    ⍝ find the 6s
      six/v         ⍝ extract them
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 5β”‚2 4β”‚3 3β”‚4 2β”‚5 1β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

Let’s write a function to find pairs adding up to a specific number. Here we’ll use Dyalog’s dynamic functions:

      pairs←{ok←⍡=+/Β¨all←, ∘., ⍨ ⍳9 β‹„ ok/all}
      pairs 6
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 5β”‚2 4β”‚3 3β”‚4 2β”‚5 1β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

There are 2 problems with this code:

#1, the rule says digits must be unique, so let’s add code to only keep the numbers that are different:

      pairs←{ok←⍡=+/Β¨all←, ∘.,⍨ ⍳9 β‹„ ok←ok ∧ β‰ /Β¨all β‹„ ok/all}
      pairs 6
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 5β”‚2 4β”‚4 2β”‚5 1β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

That’s better, but we still need to solve problem #2: some are duplicates, e.g. (1 5) and (5 1) are the same. We should remove them. Let’s create a sorting function to reorder the sets:

     Sort←{⍡[⍋⍡]}

and use it in our function to reorder each set and use unique (βˆͺ) to extract the unique ones:

      pairs←{ok←⍡=+/Β¨all←,∘.,⍨⍳9 β‹„ ok←okβˆ§β‰ /Β¨all β‹„ βˆͺ SortΒ¨ ok/all}

      pairs 6     ⍝ pairs that add up to 6
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 5β”‚2 4β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”˜
      pairs 9     ⍝ pairs that add up to 9
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 8β”‚2 7β”‚3 6β”‚4 5β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

Looks good. What about triples? We can use ∘., twice:

      i3 ∘., i3 ∘., i3    ⍝ generate a 3 x 3 x 3 of 1, 2 and 3s  
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚1 1 1β”‚1 1 2β”‚1 1 3β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚1 2 1β”‚1 2 2β”‚1 2 3β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚1 3 1β”‚1 3 2β”‚1 3 3β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚2 1 1β”‚2 1 2β”‚2 1 3β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚2 2 1β”‚2 2 2β”‚2 2 3β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚2 3 1β”‚2 3 2β”‚2 3 3β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚3 1 1β”‚3 1 2β”‚3 1 3β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚3 2 1β”‚3 2 2β”‚3 2 3β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚3 3 1β”‚3 3 2β”‚3 3 3β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
     triples←{ (⍡=+/Β¨all)/ all←, i ∘., i ∘., i←⍳9 }
     triples 6
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚1 1 4β”‚1 2 3β”‚1 3 2β”‚1 4 1β”‚2 1 3β”‚2 2 2β”‚2 3 1β”‚3 1 2β”‚3 2 1β”‚4 1 1β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

Some doubles are still there – we can’t use β‰  this time. β‰ / on more than 2 numbers is meaningless:

      β‰ /4 1 1       ⍝ same as 4β‰  (1β‰ 1) or  4β‰ FALSE!!!
1

We’ll use the nub (unique) of each set to see if it is valid with function {⍡≑βˆͺ⍡}: if the digits are unique we’ll keep the set. We’ll then sort each set and keep the unique ones:

      clean←{ βˆͺSortΒ¨ ({⍡≑βˆͺ⍡}¨⍡)/⍡ }
      triples←{all←(⍡=+/Β¨all)/all←,i ∘., i ∘., i←⍳9 β‹„ clean all}
      triples 6
β”Œβ”€β”€β”€β”€β”€β”
β”‚1 2 3β”‚
β””β”€β”€β”€β”€β”€β”˜

That’s better; we now need to write a function that will do it for any number of digits. The left argument will be the number of digits required. That means looping over ∘., until we have the proper number of iterations. In Dyalog the power operator (⍣) will help with this, it will do the looping for us:

      (i3  ∘.,  i3  ∘.,  i3) ≑ (i3 (∘., ⍣ 2) i3)
1

So we can write (for N digits we need to run ∘., N-1 times)

    NCat←{⍡ (∘., ⍣(⍺-1)) ⍡} 

Or, since the argument is the same on both sides, we can use ⍨ :

	NCat←{ ∘., ⍣(⍺-1)⍨ ⍡}
    ntuple←{ok←⍡=+/Β¨all←, ⍺ NCat ⍳9 β‹„ all←ok/all β‹„ clean all }

Let’s find how many ways we can make twelve with four different numbers:

	4 ntuple 12
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚1 2 3 6β”‚1 2 4 5β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜
      ⍴4 ntuple 12
2

Just two. There should also be only one way for 9 digits to add up to 45 (all the numbers 1 to 9):

      ⍴9 ntuple 45
WS FULL
NCat[0] NCat←{∘.,⍣(⍺-1)⍨⍡}
             ∧

Oops! Looks like we have a problem Houston. We’re trying to generate

      9 Γ— 9*9
3486784401

more than 3 billion numbers! This is too big on my machine, even with βŽ•wa=64039748.

      6 ntuple 35
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚1 4 6 7 8 9β”‚2 3 6 7 8 9β”‚2 4 5 7 8 9β”‚3 4 5 6 8 9β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      7 ntuple 39
WS FULL
NCat[0] NCat←{∘.,⍣(⍺-1)⍨⍡}
             ∧

Looking at

      i3 ∘., i3 ∘., i3
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚1 1 1β”‚1 1 2β”‚1 1 3β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚1 2 1β”‚1 2 2β”‚1 2 3β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
β”‚1 3 1β”‚1 3 2β”‚1 3 3β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
…

we can see that the numbers in the boxes are the indices of each box. APL has a primitive to produce the indices of any structure: iota (⍳) :

  (i3 ∘., i3 ∘., i3 ) ≑ ⍳ 3 3 3
1

This primitive should take less space to generate and is a lot faster than looping over :

      ntupleB←{ok←⍡=+/Β¨all←,⍳ ⍺⍴9 β‹„ all←ok/all β‹„ clean all}

Let’s see how much faster it is. There is a user command in Dyalog that allows us compare timings:

      ]runtime  "6 ntuple 35"  "6 ntupleB 35"  -compare
                                                                        
  6 ntuple 35  β†’ 2.4EΒ―1 |   0% βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•
  6 ntupleB 35 β†’ 1.4EΒ―1 | -43% βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•                 

But it still suffers from space problems:

      7 ntupleB 39
WS FULL
ntuple2[0] ntupleB←{ok←⍡=+/Β¨all←,⍳⍺⍴9 β‹„ all←ok/all β‹„ clean all}
                               ∧

But wait, maybe we can do it another way. How about using encode? Here are the 81 pairs again:

      1+ 9 9 ⊀ ¯1+ ⍳9*2
1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2

      3 4 5 6 7 8 9 1 2 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 9 
      5 5 5 5 5 5 5 6 6 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1

      9 9 9 9 9 9 9 9
      2 3 4 5 6 7 8 9

Again, that would also require too many numbers when N is greater than 6. This brute force method fails for large N, let’s see if we can modify it.

Attempt #2

Maybe we can refine the process. Let’s have a look at pairs again:

>      pairs
{ok←⍡=+/Β¨all←,∘.,⍨⍳9 β‹„ ok←ok∧ β‰ /Β¨all β‹„ βˆͺSortΒ¨ok/all}

We don’t really need Sort nor the unique (βˆͺ) after. We can eliminate a lot of cases by reordering the checks and taking into account that the numbers in the sets must be in increasing order:

      pairs2← {all←(</Β¨all)/all←, i9 ∘., i9←⍳9 β‹„ (⍡=+/Β¨all)/all}
      pairs2   9
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 8β”‚2 7β”‚3 6β”‚4 5β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

Triples are similar. We cannot use </ on 3 numbers but since the last ones are already ordered we can use </ on the first 2 . We could write

       triples2←{
	      all←(&lt;/Β¨2↑¨all) /all←, i9 ∘., (&lt;/Β¨all)/all←,i9 ∘., i9←⍳9  
	      (⍡=+/¨all)/all
	   }
      triples2  19
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚2 8 9β”‚3 7 9β”‚4 6 9β”‚4 7 8β”‚5 6 8β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

Seems to work. Quadruples would work similarly. We should use a function, like Ncat, to generate the combinations, something like

      Gen←{ ((&lt;/Β¨2↑¨all)/ all←,(⍳9) ∘., ⍡}

We can use the user command ]ROWS (newly introduced in Version 14.0 of Dyalog) to cut the output to the width of the window (paper here):

      ]rows -style=cut
Was -style=long
      Gen ⍳9
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β€’
β”‚1 2β”‚1 3β”‚1 4β”‚1 5β”‚1 6β”‚1 7β”‚1 8β”‚1 9β”‚2 3β”‚2 4β”‚2 5β”‚2 6β”‚2 7β”‚2 8β”‚2 9β”‚3 4β”‚3 β€’
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴──‒
      Gen Gen ⍳9
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β€’
β”‚1 2 3β”‚1 2 4β”‚1 2 5β”‚1 2 6β”‚1 2 7β”‚1 2 8β”‚1 2 9β”‚1 3 4β”‚1 3 5β”‚1 3 6β”‚1 3 7β”‚β€’
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴‒
      Gen Gen Gen ⍳9
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β€’
β”‚1 2 3 4β”‚1 2 3 5β”‚1 2 3 6β”‚1 2 3 7β”‚1 2 3 8β”‚1 2 3 9β”‚1 2 4 5β”‚1 2 4 6β”‚1 β€’
└───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴──‒

Let’s try it:

      ntuple2←{ ok←⍡=+/Β¨all← Gen⍣(⍺-1) ⍳9 β‹„ ok/all}
      3  ntuple2  19
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚2 8 9β”‚3 7 9β”‚4 6 9β”‚4 7 8β”‚5 6 8β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

The ultimate test: can it find the only solution to a 9 digits sequence adding up to 45 +/⍳9?

      9  ntuple2  45
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚1 2 3 4 5 6 7 8 9β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

It works! We now have a solution. Mission accomplished. Let’s see how much space is needed to run it; the user command ]spaceneeded will provide that information:

      ]space  "9 ntuple2  45"
85824

Not bad. How much CPU does it take?

      ]runtime "9 ntuple2 45" -repeat=1s

* Benchmarking "9 ntuple2 45", repeat=1s
                     Exp 
 CPU (avg):  2.835227273 
 Elapsed:    2.866477273

3 ms. That’s good enough.

Out of curiosity, could we have done better?

Attempt #3

Looking carefully at pairs we see that the first number can be 1 to 9 and that the second number can be whatever remains (as long as it is in the range 1 to 9) but not the same, i.e. for pairs adding up to a specific Sum e.g. 10, we have 1 and (10-1), 2 and (10-2), 3 and (10-3), etc. In APL ⌈:

      (⍳9) ,¨ 10-⍳9
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 9β”‚2 8β”‚3 7β”‚4 6β”‚5 5β”‚6 4β”‚7 3β”‚8 2β”‚9 1β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

The sets should be ordered in increasing order so we can limit the numbers 1 to 4 as first number because the largest combination we will have will be n followed by n+1, i.e. 10=n+n+1 or n=4.5, or 4 since we only deal with integers.

      pairs3← {i,Β¨ ⍡-i← ⍳⌊ (⍡-1)Γ·2 }
      pairs3  9
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 8β”‚2 7β”‚3 6β”‚4 5β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
      pairs3  10
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 9β”‚2 8β”‚3 7β”‚4 6β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜

Fine. How about triples? Using the same idea we find that the largest set will be n,(n+1),(n+2) so we can use the numbers from 1 to ⌊(Sum-3)÷3 followed by all the pairs of Sum minus that number. For example

      Sum←10
      (Sum-3) ÷ 3   ⍝ we start with the numbers 1 and 2
2.333333333
      1,Β¨ pairs3  Sum-1
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚1 1 8β”‚1 2 7β”‚1 3 6β”‚1 4 5β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜
      2,Β¨ pairs3  Sum-2
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚2 1 7β”‚2 2 6β”‚2 3 5β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

Not quite. We should ignore any pair starting with a number smaller or equal to our first number.

Let’s modify pairs3 to accept a (optional) left argument specifying the starting numbers to skip:

      pairs3← {⍺←0 β‹„ i,Β¨ ⍡-i← ⍺↓ ⍳⌊ (⍡-1)Γ·2 }
      pairs3  9
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚1 8β”‚2 7β”‚3 6β”‚4 5β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
      1 pairs3  9
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚2 7β”‚3 6β”‚4 5β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
      1,Β¨ 1 pairs3  Sum-2
β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
β”‚1 2 6β”‚1 3 5β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

      2,Β¨ 2 pairs3  Sum-2
β”Œβ”€β”€β”€β”€β”€β”
β”‚2 3 5β”‚
β””β”€β”€β”€β”€β”€β”˜

Triples?

      triples3←{i← ⍳⌊ (⍡-3)Γ·3 β‹„ i,Β¨ i pairs3Β¨ ⍡-i }
      triples3  9
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚β”Œβ”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”β”‚β”Œβ”€β”¬β”€β”€β”€β”β”‚
β”‚β”‚1β”‚2 6β”‚3 5β”‚β”‚β”‚2β”‚3 4β”‚β”‚
β”‚β””β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜β”‚β””β”€β”΄β”€β”€β”€β”˜β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

Not quite, we need to use each (Β¨) twice:

      triples3←{i← ⍳⌊ (⍡-3)Γ·3 β‹„ i,¨¨ i pairs3Β¨ ⍡-i }
      triples3  9
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”β”‚β”Œβ”€β”€β”€β”€β”€β”β”‚
β”‚β”‚1 2 6β”‚1 3 5β”‚β”‚β”‚2 3 4β”‚β”‚
β”‚β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜β”‚β””β”€β”€β”€β”€β”€β”˜β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

That’s better but this enclosing business is getting out of hand. Let’s work with matrices:

      pairs3B←{⍺←0 β‹„ i,βͺ⍡-iβ†βΊβ†“β³βŒŠ(⍡-1)Γ·2}
      pairs3B  9
1 8
2 7
3 6
4 5
      2 pairs3B  9
3 6
4 5
      triples3B← {i← ⍳⌊ (⍡-3)Γ·3 β‹„ ↑βͺ/i,Β¨ i pairs3BΒ¨ ⍡-i }
      triples3B  9
1 2 6
1 3 5
2 3 4

Seems to work. But there is a pattern here. It looks like a recursive definition.

  • If we want a single digit set then the set is the sum if it is below 10.
  • If we want a N digit set then it is all the digits from 1 to ⌊(Sum-+/⍳N-1)Γ·N followed by the N-1 digit set of Sum minus that number.

We’ll need to adjust the starting number and remove any number smaller or equal to the first digit. We need to supply that number as argument, something like:

     ntuple3← {
  ⍝ Generate all monotonic combinations of ⍺ numbers adding to ⍡
     (nn sn)←2↑⍺      ⍝ # of numbers needed, numbers to skip
     nn=1 : (⍡≀9)⌿ βͺ⍡ ⍝ solution for 1 number is ⍡ if ≀9
  ⍝ More than 1 #, drop any number ≀ sn
     n1←sn↓⍳8⌊0⌈⌊(⍡- +/ ⍳ nn-1)Γ·nn ⍝ all possible starting #
     0∊⍴n1 : 0 nn⍴0  ⍝ no solution?
  ⍝ All are starting # followed by the new combination
     ↑βͺ/ n1 ,Β¨ ((nn-1),Β¨n1) βˆ‡Β¨ ⍡-n1
 }

This solution returns a matrix instead of a list of vectors. Let’s try it:

      4 ntuple3  12
1 2 3 6
1 2 4 5
      9 ntuple3  45
1 2 3 4 5 6 7 8 9

How does it compare with the previous solution?

      ]runtime "9 ntuple2  45 " "9 ntuple3  45" -compare
                                                                         
  9 ntuple2 45  β†’ 2.9EΒ―3 |   0% βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ•βŽ• 
* 9 ntuple3 45  β†’ 5.3EΒ―5 | -99% βŽ•                                        
      ]space  "9 ntuple3  45"
3620

Quite a difference. With just a little more effort we improved our original solution a lot. And there are even faster solutions.

Conclusion

APL provides us with an environment where we can experiment β€œhands on” to study situations, verify results and make better decisions. With it we can even come up with prototypes that we can use to make further forays into our problem. There is a lot of thinking that could have been done mentally but being able to use the computer really is a bonus.

If you want, there is a video accompanying this article you can have a look at. Go to YouTube and look for β€œBrute force method to finding all the sets in a row of Kakuro”. You can also try this link: http://youtu.be/bJssWsdXjmY

Have fun!

References


  1. Kakuro