Recreational APL
Eugene McDonnell
Winter 1977

Spirals and Time

In this issue I write about the number spiral and the Gregorian calendar. In discussing them I shall use the ⍺⍵ notation used by Iverson in his recent book [1].

The ⍺⍵ notation has many advantages in exposition. By removing as much as possible of the programming content, the reader is free to concentrate on the mathematical aspects of what is being presented. The notation is used as follows: the name of the function is given first, then a colon, and then the body of the function. The left and right arguments to the function are represented in the body by the symbols and , respectively. In a monadic function the argument can be represented by either symbol. For example, a function to determine square roots could be written sqrt:⍵*0.5 . A function to determine the overtime pay (time and a half) for a worker whose rate isand who worked a total ofhours could be written as ot:⍺×1.5×0⌈⍵-40 .

A function body can be in three parts, separated by colons, to permit conditional execution. In this form the central part is executed first. If the result is 1, the right part is executed; if 0, the left part is executed. A recursive function to compute the greatest common divisor of its arguments could be written as gcd:(⍺|⍵)gcd⍺:0=⍺:⍵ .

The Number Spiral

The long-time problems editor of this journal, Bob Smith (of Scientific Time Sharing Corporation) recently found a delightful use of the scan operator in connection with the number spiral. The number spiral is an arrangement of the integers in a spiral fashion. This can be done in a number of ways; I shall use a clockwise spiral, beginning with 0. The spiral of order n has n windings. For example, the spiral of order 9 looks like this:

      16 17 18 19 20
      15  4  5  6 21 
      14  3  0  7 22
      13  2  1  8 23
      12 11 10  9 24

At the end of this article, I’ll present functions that permit the generation of spirals of order n . You may want to try your hand at your own solutions before looking at mine. Here are three problems in connection with the number spiral:

1.  Write a function ls which takes a positive integer argument n and gives as its result the length of a side (that is, the number of rows) in the spiral of order n . The result of ls 9 is 5.
2.  Write a function cn which takes a positive integer argument n and gives as a result the number of elements in the spiral of order n . The result of cn 9 is 25.
3.  Write a function sp to generate the spiral of order n for any positive integer n . Remember, the numbers in the result should wind around clockwise. If you can, write the function so that it works in either index origin.

Bob Smith’s finding has to do with the elements that appear at the turns in the number spiral. In the spiral of order 9 , these numbers are 0 1 2 4 6 9 12 16 20 . Can you think of an expression that generates these numbers? Bob’s is -\-\⍳9 . Isn’t that pretty? (0-origin indexing).

How Long Is a Year?

The problem the calendar designer faces is to make a civil artifact, in which the unit is the day, so that it faithfully measures the journey of the sun from one vernal equinox (or some other convenient point) to the next. Since this journey does not take an integral number of days, the calendar maker must insert an extra day in the year from time to time. This action is called intercalation.

Astronomers have long known fairly accurately how long the physical year is. Hipparchus of Rhodes, in about 150 B.C., measured the year at 365.242 days. The current estimate puts it at 365.242199 days, which is accurate to about one-tenth of a second.

The Gregorian calendar, which was first put into use in 1582, attempted to correct deficiencies in the Julian calendar. The Julian calendar had three common years of 365 days, followed by one leap year of 366 days. We can obtain the year length by (+/365 365 365 366)÷4 , which gives 365.25. The difference between 365.25 and 365.242199 produced enough error so that the calendar eventually was out of phase with the seasons. By 1582 the discrepancy was 9 days.

The function op:÷⍵-365.242199 tells us how many years it will take a calendar whose year has length to become one day out of phase with the physical year. The sign of the result indicates whether the calendar year is too long (+) and needs fewer intercalated days, or too short (-) and needs more intercalated days. The value of op 365.25 is 128.1877 years. The Julian calendar year was thus too long.

The solution provided by the Gregorian calendar was a compound one. It said that every fourth year shall be a leap year unless the year is divisible by 100, in which case it should be a common year, unless it is also divisible by 400, in which case it should be a leap year.

A function to determine if a given year is a common year or a leap year uses a wonderful inner product. The result of ly:0≠.=4 100 400∘.|⍵ is 0 ifis the number of a common year, and 1 if it is a leap year. This inner product is so wonderful that it was turned down for inclusion in the first edition of the Gilman and Rose book on APL [2], because it would have been too much for the book’s intended audience at the point in the book where it would have had to appear!

How long is the Gregorian year? We can answer this question with the help of the ly function. In any 400-year period the number of days is precisely the same. We need only to compute 365+.+ly⍳400 , which gives 146097, and to divide this by 400, which gives 365.2425. The Gregorian calendar will be one day out of phase with the physical year in op 365.2425 , or 3322.2591 years. The Gregorian calendar, like the Julian calendar, is too long.

There is nothing in the Gregorian calendar, as now instituted, to handle this excess. Let’s see what we might do to remedy this defect. The multiple of 400 nearest to 3322.2591 is 3200. Let’s propose that every year divisible by 3200 be a common year. The ly function would be modified to ly:0≠.=4 100 400 3200∘.|⍵ ; and the length of the year would be (365+.+ly⍳3200)÷3200 , or 365.2421875 days. A calendar with a year this long would become out of phase by one day in op 365.2421875 , or 86956.52153 years, and the year would be too short. How shall we handle this? Well, the nearest multiple of 3200 to 86956.52153 is 86400. Let’s propose then that a year divisible by 86400 be a leap year. The ly function now would be ly:0≠.=4 100 400 3200 86400∘.|⍵ ; and the length of the year would be (365+.+ly⍳86400)÷86400 , or 365.2421991 days. The result of op 365.2421991 is 13500009.24, which means that the calendar would be one day out of phase in a little over thirteen and a half million years.

Do you feel like quitting? I do. By the way, do any of you recognize 86400? It is also the number of seconds in one day!

Back to the Number Spiral

I solved the three problems like this:

1.  ls:⌈⍵*2
2.  cn:⌊(⍵*2)÷4
3.  sp:(⊖⍉sp⍵-1),(cn⍵)+⍳ls⍵:⍵=1:1 1⍴⎕io

Notice that the result of cn ⍳9 is 0 1 2 4 6 9 12 16 20 , the same result obtained by -\-\⍳9 using 0-origin indexing. In the function sp , the expression ⊖⍉ is used to give its argument a quarter-turn counterclockwise rotation. Appending the interval vector (cn⍵)+⍳ls⍵ to it gives the desired result. The recursion is terminated when the argumentis equal to 1, in which case the starting matrix 1 1⍴⎕io is the result.


1.  Iverson, K. E., Elementary Analysis, APL Press, Swarthmore, Pa., 1976.
2.  Gilman, L., and Rose, A. J., APL: An Interactive Approach. Wiley, New York, 1970.

First appeared in APL Quote-Quad, Volume 7, Number 4, Winter 1977. The text requires the APL385 Unicode font, which can be downloaded from . To resolve (or at least explain) problems with displaying APL characters see .

original writing:  2009-05-25 22:40
last updated:2010-08-19 20:35