Current issue

Vol.26 No.4

Vol.26 No.4


© 1984-2024
British APL Association
All rights reserved.

Archive articles posted online on request: ask the archivist.


Volume 24, No.1

  • Proof for author
  • 0.3

Functional calculation

2: The year 1997

by Neville Holmes (

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.


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.


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.


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.


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.


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.


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 11:07:53
caching off
debug mode off
cache time 3600 sec
indmtime not found in cache
cached index is fresh
recompiling index.xml
index compiled in 0.1891 secs
read index
read issues/index.xml
identified 26 volumes, 101 issues
array (
  'id' => '10500050',
regenerated static HTML
article source is 'XHTML'
completed in 0.2143 secs