# APL — solving Euler problem 1

Note: see the rest of the series so far.

Sur­pris­ing­ly, my last post on APL did well on Hack­erNews, turns out there is gen­uine inter­est in APL out there, which is great to see

I have been busy with a num­ber of con­fer­ences and talks since then so I haven’t had time to revis­it APL. Over the next cou­ple of posts in this series (or rather, what I hope would amount to a series) let’s try to solve some sim­ple prob­lems with APL. Mind you, my knowl­edge of APL is pret­ty shal­low so what I end up with is like­ly far from idiomat­ic APL code, so feel free to offer pointers/feedbacks in the com­ments sec­tion below!

To start us off nice and easy, let’s look at some­thing real­ly sim­ple, like Euler Prob­lem 1.

If we list all the nat­ur­al num­bers below 10 that are mul­ti­ples of 3 or 5, we get 3, 5, 6 and 9. The sum of these mul­ti­ples is 23.

Find the sum of all the mul­ti­ples of 3 or 5 below 1000.

Super easy, right? I knew how to solve this with a one-lin­er even when I was a total begin­ner in F#.

which rough­ly trans­lates to the fol­low­ing in APL:

$F \ \iota 9$ => 23

Looks great, and run­ning $F \ \iota 999$ gives the right answer (I won’t spoil the answer for you, but feel free to run this in TryAPL at your own dis­cre­tion!) So here’s what we did (p.s. | is the remain­der oper­a­tor):

1. find ele­ments from the input $\omega$ that are divis­i­ble by 3 $(3|\omega)=0$ or 5 $(5|\omega)=0$
2. which gives us an array of booleans, which we then OR $\lor$
3. then we use com­pres­sion to fil­ter the input array $/ \omega$ to return only the ele­ments that are divis­i­ble by 3 or 5
4. and final­ly reduce over these ele­ments with addi­tion

p.s. OK, it’s not exact­ly a faith­ful trans­la­tion of the F# code above, the equiv­a­lent F# code should prob­a­bly use reduce as well:

UPDATE 24/07/2015 : much to my pleas­ant sur­prise this piqued Tomas’s inter­est and he even did a quick AplMode to show how you can mim­ic the APL code above in F# and solve the prob­lem in 19 char­ac­ters.

To be fair, I’m writ­ing APL whilst think­ing like a F#er so I’m not doing it jus­tice, but Ste­fano shared an even short­er APL solu­tion (17 char­ac­ters):

This took me a lit­tle while to under­stand, but for­tu­nate­ly Dya­log APL’s IDE’s real­ly quite easy to use and so after some exper­i­men­ta­tion here’s how Stefano’s solu­tion works:

(first you need to under­stand that in APL, you always read from RIGHT-TO-LEFT unless you can ( ))

1) gen­er­ate an out­er prod­uct ($\circ .$) of reminders (using the residue | oper­a­tor):

• the array of 3 and 5; and
• input array $\omega$

this gen­er­ates (for 1 to 9) the matrix:

1  2  0  1  2  0  1  2  0

1  2  3  4  0  1  2  3  4

2) trans­form matrix into boolean by com­par­ing them to 0 (0 = …)

0  0  1  0  0  1  0  0  1

0  0  0  0  1  0  0  0  0

3). so now we reduce the matrix using the log­i­cal OR oper­a­tor (the spe­cial sym­bol you see here is called reduce first and is the same as reduce / but along the first dimen­sion of the array)

0  0  1  0  1  1  0  0  1

4) since this array has the same length as our input, we can just mul­ti­ply the two togeth­er!

0  0  3  0  5  6  0  0  9

5) and final­ly reduce over this array using addi­tion and voila!