﻿ Vector, the Journal of the British APL Association

## Volumes

British APL Association

Archive articles posted online on request: ask the archivist.

Volume 24, No.1

• Proof for author
• 0.3

# by Neville Holmes (neville.holmes@utas.edu.au)

Functional calculation does with operations applied to functions and numbers what numerical calculation does with functions applied to numbers. In preceding articles an introduction was given to what could be done with one commonly available tool for functional calculation, using a notation called J, then details were given of simple numerical calculation. This article is intended to allow the reader to consider how simple numerical calculation can be done in J by showing numeric expressions to produce whole numbers below `100` starting from the digits of the number `1997`.

## Numerical calculations

Preceding articles have introduced numerical calculation using the interpreter for the J notation. This article gives a change of pace in which the notation already introduced is used in modest examples so that the reader can get used to its differences from the more usual (though inconsistent) mathematical notation.

To set up some simple examples, expressions to produce all the non-negative integers of fewer than three digits are to be sought. There are several restrictions.

Firstly, only the following scalar primitive functions are to be used, though clearly many of them are not very useful – there are more comparisons than are needed, and nand and nor have very restricted domains.

 + conjugate add +. GCD +: double nor - negate subtract -. not -: halve * signum times *. LCM *: square nand % reciprocal divide %: square root root | magnitude residue ^ exp power ^. loge logarithm = equal < less than <. floor lesser <: decrement not more > more than >. ceiling greater >: increment not less ~: not equal ! factorial choices o. pi times circular p: prime [ (same) (left)

Secondly, only the digits `1 9 9` and `7` must be used as arguments, all those digits, and only in the sequence given. Thus up to four arguments may be used in an expression, but no argument may be a list, that is, all arguments must be scalar. The significance of this will be clearer later.

In the following, two expressions are given for each number, both because there is room for them, and to add interest. The reader should, as an exercise, look for solutions better in some way than those given – there should be plenty, and looking for them should lead to insights, whether the search is successful or not.

One avenue to explore is the use of numbers other than simple integers. These are avoided in the examples but, for instance, `19+<.|9j7` gives `30` using only three functions, which is fewer than in the expressions given below. Similarly, `>.%:1p9-9p7` uses fewer functions to get `52` than do the examples.

## Making 1997 give 0 to 19

Taking the four digits `1 9 9` and `7`, in that sequence, combine them using J functions and operations, in as short and simple an expression as possible, to yield each of the numbers between `0` and `19` (here, `99` later), and to yield them as scalars using only scalar arguments and scalar functions. (As a matter of aesthetics, parentheses and decimal points are also avoided as far as possible. Also as a matter of style, the negative sign is avoided and subtraction or negation is used to similar effect.)

 0 19=97 ˆ.*1997 10 19-9>.7 |1-9+9-7 1 *1997 199>7 11 19-9-*7 19--:9+7 2 19|97 +:*1997 12 1+9+9-7 19-9|7 3 19-9+7 199-*:+:7 13 -:19+9|7 1*9+%:9+7 4 19|9ˆ7 1+%:9s<.97 14 199|+:7 1+9+%:9+7 5 19-+:9|7 19|>.ˆ.97 15 >.19%9%7 1+9+>.ˆ.97 6 19|9*7 1+9-%:9+7 16 19|9+7 <.19*9ˆ.7 7 1!9-9-7 19|9!+:7 17 1+9+9|7 19-9-7 8 1+9|97 19|%:>:9*7 18 19-*97 1+9+9-*7 9 19<.9>.7 19-9+*7 19 1*p:9-9-7 1+9+9>.7

Two expressions are given in the table for each number. Several expressions use only the one primitive function, but none needs more than four.

Expressions abound for `0` and `1`, particularly expressions using functions that yield logical values. Thus `1>997` and `199<7` yield `0`, while `19<97` and `19+.97` yield `1`. The restriction of yielding a scalar rules out `/:1997` and `=1997` which otherwise give a quite respectable `0` and `1`. Also, `#1997` gives a scalar `1`, and `#19 97` gives a scalar `2`, but the restriction on scalar components rules the second of these two possibilities out of order.

Certain components of expressions are repeatedly used in the table above. Thus, `199|` (and `19|`) is used to pass over the digits of the left argument of `|` when the value of its right argument is lower than that of its left. In particular, `9|7` simply yields `7`. Otherwise, `<.` or `>.` are widely used where the digits to the right or left of the `<.` or `>.` are to be ignored and the simpler and more pleasing `|` won’t serve instead. Notice, though, that the instances of `19|` in `19|9ˆ7` and `19|9*7` and `19|9!+:7` are not ignorant.

Expressions starting with monadic increment (`>:`) and decrement (`<:`) functions can often make a shorter expression than the example given, but since these will only be trivially different from neighbouring expressions they are avoided if possible. Monadic halve (`-:`) and double (`+:`) are useful, but these are also a bit trivial for generating smaller or larger numbers.

The year 1997 is interesting in the scope it gives for the square root function (`%:`). In the above, `%:9<.97` is used to give `3`, `%:9+7` is used to give `4`, and `%:>:9*7` is used to give `8`. In fact `-:9+7` and `9-*7` also give `8`, but maybe not so cutely. Otherwise, `>.*:1.997` could have been used for `4` and `>.ˆ.1997` for `8`, both of which are quite interesting.

## Making 1997 give 20 to 99

Making numbers beyond 19 follows a similar pattern, and it is convenient here to take them twenty at a time. Of course, expressions starting 19+ will be common in the next table.

 20 19+9>7 19+*97 30 19+9++:*7 1+9++:9+*7 21 19+9-7 19+>:*97 31 <.19*9%:7 <.%:-:1997 22 <.-:%:1997 >:19+9-7 32 1+<.%:997 +:19|9+7 23 19+%:9+7 p:1+9-9-7 33 >.19ˆ.ˆ97 1+9>.+:9+7 24 19-9-+:7 19+p:9+7 34 +:19-9-7 1!<.9*9ˆ.!7 25 19+9|<:7 1!9+9+7 35 19+9+7 >.19+ˆ.9ˆ7 26 1+9+9+7 19+9|7 36 +:19-*97 1!9*>.9ˆ.!7 27 19+-:9+7 19+9-*7 37 1+-:9+9*7 >.ˆ.199ˆ7 28 19+9>.7 1+99|!<:7 38 1+p:9+9-7 19*9-7 29 >.199%7 19+9+*7 39 -:-19-97 <:+:19+9>7

At a pinch, all the numbers here, and in the next table for that matter, can be constructed from the solutions of the previous table by doubling (monadic `+:`) possibly combined with incrementing (monadic `>:`) or decrementing (monadic `<:`). However, these solutions will only reluctantly be used here, in the absence of some other expression.

Getting these larger values is somewhat more difficult, so it useful more often to go to non-integer intermediate values and then use the floor (`<.`) or ceiling (`>.`) function to get an integer. There are no solutions given in the 20-39 table which need only the one function, but quite a few need only two. On the other hand, no solutions need more than five functions.

 40 +:19+*97 1-9-<.-:97 50 -:1+99>.7 1-9-9+*:7 41 1!<.9*ˆ.97 1+9+-:<:9*7 51 1+99-*:7 19++:9+7 42 +:19+9-7 19+9++:7 52 +:1+9+9+7 1+>.99%ˆ.7 43 >.%:19*97 1*<.9*9ˆ.!>:7 53 1*-:99+7 |1+9-9*7 44 -19-9*7 <.%:1997 54 1+-:9+97 -.1+9-9*7 45 >.%:1997 >.ˆ.19!97 55 1-9-9*7 -.1*9-9*7 46 1!-:99-7 19+9+p:7 56 1+-.9-9*7 19+>:9*-:>:7 47 1+-:99-7 1!>.9*ˆ.+:97 57 -:1+99++:7 19*<.9ˆ.!7 48 1+>.*:ˆ.997 -1-9-9-*:7 58 -:19+97 19>.9+*:7 49 *:199<.7 -:1+9>.97 59 1+9+-:>:97 19-9-*:7

For the 40s and 50s, `9*7` gives 63 to work down from, and `-:97` and `-:99` gets into the high 40s, as does `*:7`. At least one expression is given for each of these numbers which does not need more than four primitive functions.

 60 >.19*o.9>7 1+<.+:%:9*97 70 +:19+9+7 |1-<.9*%:9*7 61 -:199|!>:7 >.%:+:19*97 71 -1-9+9*7 -:>.ˆ.%:19ˆ97 62 19>.<:9*7 -:>:199|!<:7 72 -.1-9+9*7 1*-:9*9+7 63 1>.9*9<.7 -.1-9>.9*7 73 1+9+9*7 19+9*<:7 64 1+9>.9*7 1+9+9*<:7 74 >.19*9ˆ.!7 <.19++:ˆ.9!7 65 199|!7 199|!+:7 75 -:199-*:7 19+9+p:+:7 66 -1-9+9+*:7 1*<.9*%:9*<:7 76 19*%:9+7 19*>.9ˆ.!7 67 19+<.-:97 19+-:<:97 77 >.19*o.9%7 >.+:1+%:99*7 68 19+>.-:97 19+-:>:97 78 -19-97 19+p:9+7 69 >.19*ˆ9%7 19++:9++:7 79 -.19-97 >.19*ˆ.9*>:7

For the 60s and 70s, `9*7` provides a good starting point. Of interest are the two expressions for 65, which look the same but are interestingly different, and otherwise only 78 and 79 have expressions with only two primitive functions. There is plenty of room for improvement here, though only 66 is shown as needing more than five primitive functions.

 80 1-+:9--:97 1*99-p:7 90 >.+:%:1997 19+<.9ˆˆ.7 81 1>.9*9>.7 -.1-9+9*>:7 91 1+99|!7 -1-99-7 82 19+9*7 1+9+9*>:7 92 1!99-7 -.1-99-7 83 >:19+9*7 1++:<.9*ˆ.97 93 1+99-7 1--:>:9-+:97 84 +:+:19+9-7 1+>.+:9*ˆ.97 94 >:1+99-7 1--:<:9-+:97 85 1!99-+:7 <.19*ˆ9%<:7 95 19*>.ˆ.97 19*>:%:9+7 86 <.19*ˆ.97 1+99-+:7 96 -1-9>.97 -:199-7 87 |1+9-97 >.19*ˆ.97 97 19>.97 |1-99-*7 88 -.1+9-97 >.1*9*%:97 98 1+9>.97 -.1-99-*7 89 1-9-97 -:<:199|!>:+:7 99 1>.99>.7 -:199-*7

The 80s and 90s often use 99 or 97 and work down from there. This gives the quite short expressions shown here for 89 and 97, though `9*7` and 199 find occasional use.

In this group of numbers there is the one expression with only one primitive function, seven with two, lots with three, but the improvement to be sought is in those with five or six primitive functions.

## Further examples

The examples given above can only suggest how arithmetic functions can be used in a simple to produce a variety of numbers. The reader is urged to consider the examples above with a J interpreter to hand, to try the examples out, to check them, and to try to find expressions that are better or in some way more interesting than those given here. When generating these numbers begins to pall, the reader perhaps should go on to consider how to generate the three digit numbers using the same rules. This could start `1+99>.7` then `199-+:*:7`.

Alternatively, expressions might be sought for other years. Some years will present special challenges. The following table gives a start for the year 2000, in which a choice is made between `0=0`, `0!0` and `0ˆ0` to give a 1 largely on aesthetic grounds.

 0 200=0 ˆ.*2000 10 20%+:0=0 20->.o.o.0!0 1 *2000 200ˆ0 11 -:20++:0!0 <.ˆ+:*:200+0 2 2+0-0*0 +:*2000 12 20-<.o.ˆ0=0 +:<.%:%:2000 3 2+0=0+0 2!>.0+ˆ0=0 13 <.+:%:%:2000 p:<.ˆ.200-0 4 2*+:0<0!0 >.ˆ.20+0=0 14 <.%:200+0 <.ˆ.-:*:2000 5 <.ˆ.200+0 20-<.ˆˆ0=0 15 >.%:200-0 <.+:ˆ.2000 6 <.%:%:2000 >.ˆ.200+0 16 >.ˆ.*:2000 20->.o.0=0 7 <.ˆ.2000 >.ˆ.-:2000 17 20->:+:0!0 >.+:ˆ.+:2000 8 2ˆp:0+0!0 2ˆ0]>:+:0=0 18 20-+:0=0 >.!o.%:2000 9 <.-:20-0!0 >.ˆ.p:2000 19 20-0!0 >.ˆ.p:*:2000

Apart from reducing the number of possibilities, having all those nought digits tends to give unsightly numbers like `00` and `000` which are therefore avoided here as far as is convenient. Thus, although `2+000` would be technically correct for 2, the expression `2+0-0*0` is shown, though many similar ones would be just as satisfactory. Not having a 1 at the front also reduces the possibilities a lot!

Another amusing possibility, though ultimately monotonous because expressions are restricted to monadic functions, is to try to develop all the numbers from only a single zero. A start to this is given in the following, and the aesthetic choice is between `!0`, `ˆ0`, `>:0`, and `-.0` to get the initial 1, or `p:0` to get an initial 2.

 0 |0 *0 10 >.*:o.!0 >.o.o.>:0 1 !0 ˆ0 11 <.-:ˆo.ˆ0 p:+:p:0 2 +:-.0 p:0 12 -:!+:+:!0 -:>.ˆo.-.0 3 p:-.0 >.ˆˆ0 13 <.*:>:ˆˆ0 p:p:p:0 4 *:+:!0 >.o.>:0 14 +:<.!o.!0 +:<.*:ˆˆ0 5 >:+:+:>:0 p:p:0 15 <.ˆˆˆ0 <:*:*:+:!0 6 !>:+:!0 +:>:+:-.0 16 *:*:>:>:0 *:+:+:-.0 7 <.ˆ+:ˆ0 p:p:ˆ0 17 <.o.+:ˆˆ0 p:+:p:ˆ0 8 <.o.ˆˆ0 >.ˆp:0 18 +:>.o.ˆˆ0 +:*:>:+:!0 9 *:>:+:!0 >.o.ˆˆ0 19 <:<.ˆ>.ˆ!0 p:p:>:p:0

The shortness of some of expressions given here comes often from using the `o.` and `ˆ` functions to bump up values quickly, then using floor (`>.`) or ceiling (`<.`) functions to get integers. Otherwise `p:` can be used most conveniently. Of course, where an expression starts with `<.` the next higher number can be got by substituting a `>.` and vice versa, mutatis mutandis.

The expressions given in these last two tables were selected unsystematically and hastily. The assiduous reader should have an interesting but fruitful time looking for improved and extended expressions.

```script began 1:10:11
caching off
debug mode off
cache time 3600 sec