1 min read

APL Hacking: Project Euler Daily (#16)

For this problem, I could have reused my CARRY function from before, but it seemed like it would only make things more difficult. Instead, I realized that there was a really simple carry algorithm that I could use for this problem. I also did not want to use the Java bignum support since I had already done that in a previous problem. It is much more fun to do this in pure APL.

As a note on Control structures, I have found that I actually have a harder time reading the control structure based code than the code with branches. It seems to me that the control strucutres for problems of this size don't lend anything to readability. Rather, the density suddenly changes between fairly low to failure high as I move from normal APL to the control structures, and that usually makes my code read like Swiss cheese. For problems of this size, I find that using labels with branches seems to keep the density fairly constant while lending me hints in the code that I don't get from the control structures. Admittednly, if I through in more comments, I could probably get the same, but then the density goes down even further.

Problem #16:

∇R←PESIXTEEN;X;C;POW

⍝ Compute the sum of the decimal digits of 2*1000

POW←1000
R←1 ⋄ I←0
ADD:R←2×R ⋄ C←⌊R÷10
→(~1↑C)/CARRY ⋄ R←0,R ⋄ C←0,C
CARRY:R←(1⌽C)+10|R
I←I+1 ⋄ →(I<POW)/ADD
R←+/R