Diamond for APL under the IBM-5110
Author: dmsc
Category: Christmas Challenge
System: IBM 5110
Language: APL
Len source code: 27
Len exe file: N/A
Len code only: N/A
Instructions:
You can run the program using the web based IBM 5110 emulator at
https://norbertkehrer.github.io/ibm_5110/emu5110.html
In the emulator, switch to APL mode with the switch at the upper right, and
then push the restar button.
The screen of the IBM 5110 computer is only 64x16 characters, to see the full
height of the pattern in a real computer you would need to connect a printer
and see the printout.
Description:
I decided to write an APL implementation of the challenge after seeing the
last year entry by Peter De Watcher.
The full program is:
' *'[1+3=6|V∘.-V←(5+⍳19)*2]
The code explanation is the following:
(5+⍳19) : This gives the 19 numbers from 6 to 24
*2 : Each number is squared: 36 49 64 ... 576 In modern APL
this could be written 2*⍨5+⍳19, avoiding parenthesis,
or even shorter ×⍨5+⍳19.
V← : The result is stored in the variable V = 36 49 ... 576.
V∘.- : The "outer product" of the subtraction of each element
of V and the list above.
This gives a matrix:
0 -13 -28 ... -540
13 0 -15 ... -527
28 15 0 ... -512
... ... ... ... ...
540 527 512 ... 0
6| : Takes the modulo 6 of the numbers. Now, each position with
a star is exactly 3:
0 5 2 3 2 5 ... 0
1 0 3 4 3 0 ... 1
4 3 0 1 0 3 ... 4
3 2 5 0 5 2 ... 3
...
3= : Selects all values equal to 3, so now we only have 1 or 0:
0 0 0 1 0 0 ... 0
0 0 1 0 1 0 ... 0
0 1 0 0 0 1 ... 0
1 0 0 0 0 0 ... 1
...
1+ : Adds 1, so now we have 1 (space) or 2 (star).
' *'[ ] : String indexing - replace each 2 with the '*' symbol and
each 1 with a space.
There are multiple variations of the same algorithm, for example:
Subtracting 1 instead of adding 5 in at the start:
' *'[1+3=6|V∘.-V←(1-⍳19)*2]
Using a temporary location "U" instead of the parenthesis:
' *'[1+3=6|V∘.-V←U×U←5+⍳19]
Using multiple values instead of the 3=:
' * '[1+6|V∘.-V←(5+⍳19)*2]
Modern APL version, shorter by 4 characters (23 symbols):
' *'[1+3=6|∘.-⍨×⍨5+⍳19]