Soni's Blog

The k-odd and k-even numbers

We made a "game".

Except it's not really a game. Instead, it's an interactive tool we came up with to help us explain how we solve some problems in the real world, sometimes. In this blog post, we aim to explore these problems and our approach to them, and what we can learn from them.

Let's say you have a Programmable Logic Controller (PLC) with (to keep it reasonable) 16 outputs. While the PLC likely has a test mode that makes it easy to activate each output in sequence, let's assume you do not know about it, but you do know how to write and run a program. Now, loading the program onto the PLC is likely the slowest part of the process, so doing them one at a time is, to say the least, highly suboptimal. Instead, we can start building a table of actuators and outputs, filling out the actuators first:

Red Indicator
Yellow Indicator
Green Indicator
Left Arm X
Left Arm Y
Left Arm Z
Left Arm Grab
Left Arm Flip
Right Arm X
Right Arm Y
Right Arm Z
Right Arm Grab
Right Arm Flip
Left Reject
Right Reject

Next up, we activate these in groups of 8 (half of our total outputs). The groups can be rather arbitrary, as long as they follow a specific overlap pattern, tho by using what we're gonna be defining as k-odd or k-even numbers we can simplify it a lot, with k-odd being the easiest to use. We define k-odd numbers as being numbers that satisfy (x mod 2k) - (x mod k) = k, for example the 1-odd numbers are just (x mod 2) - (x mod 1) = 1 where x mod 1 simplifies to zero, leaving us with x mod 2 = 1 (aka the odd numbers). Likewise, k-even numbers are those that satisfy (x mod 2k) - (x mod k) = 0 (for 1-even, simplifies to the even numbers).

Since we're working with groups of 8, it is convenient to start with 8-odd numbers and go down from there, to 4-odd, to 2-odd, and finally to 1-odd, as it makes the readout easier (as binary numbers). The 8 first 8-odd numbers are: 8, 9, 10, 11, 12, 13, 14, and 15. We can activate these outputs, and write down a "1" for the actuators that moved, and "0" for the actuators that didn't:

Red Indicator1
Yellow Indicator1
Green Indicator1
Left Arm X0
Left Arm Y0
Left Arm Z0
Left Arm Grab1
Left Arm Flip0
Right Arm X0
Right Arm Y0
Right Arm Z0
Right Arm Grab1
Right Arm Flip0
Left Reject1
Right Reject1

Next up, we do the 8 first 4-odd numbers, they are: 4, 5, 6, 7, 12, 13, 14, and 15. Note that 4 of these overlap with the previous group. Then we note down a "1" on what activated, and a "0" on what didn't:

Red Indicator11
Yellow Indicator11
Green Indicator11
Left Arm X00
Left Arm Y00
Left Arm Z01
Left Arm Grab10
Left Arm Flip01
Right Arm X00
Right Arm Y00
Right Arm Z01
Right Arm Grab10
Right Arm Flip01
Left Reject10
Right Reject10

Now for the 2-odd numbers: 2, 3, 6, 7, 10, 11, 14, and 15. This time, 6 of these overlap with previous groups. We once again note them down, same as before:

Red Indicator111
Yellow Indicator111
Green Indicator110
Left Arm X000
Left Arm Y000
Left Arm Z010
Left Arm Grab100
Left Arm Flip011
Right Arm X001
Right Arm Y001
Right Arm Z010
Right Arm Grab100
Right Arm Flip011
Left Reject101
Right Reject101

Finally we are left with the odd numbers: 1, 3, 5, 7, 9, 11, 13, and 15. 7 of them overlap with previous groups. After noting them down, as before:

Red Indicator1111
Yellow Indicator1110
Green Indicator1101
Left Arm X0000
Left Arm Y0001
Left Arm Z0100
Left Arm Grab1000
Left Arm Flip0110
Right Arm X0010
Right Arm Y0011
Right Arm Z0101
Right Arm Grab1001
Right Arm Flip0111
Left Reject1010
Right Reject1011

We can now read these out in decimal and sort them around:

Left Arm X0
Left Arm Y1
Right Arm X2
Right Arm Y3
Left Arm Z4
Right Arm Z5
Left Arm Flip6
Right Arm Flip7
Left Arm Grab8
Right Arm Grab9
Left Reject10
Right Reject11
Green Indicator13
Yellow Indicator14
Red Indicator15

This gives us our PLC output allocation table, and we only had to program our PLC 4 times, not 16! Also note that one of these never got activated, and yet we still found its output address.

The usefulness of the k-odd numbers is that we can easily go further, we can take it up to 32, 64, 128, even 256 outputs, or more. For those trying to play the "game" we introduced above, k-odd numbers are basically necessary for playing it fast, and indeed, we observed their usefulness by trying to optimize how we played it ourselves.

We hope others may find k-odd and k-even numbers useful, too!