Skip to content

Instantly share code, notes, and snippets.

@tormaroe
Last active December 12, 2019 09:22
Show Gist options
  • Save tormaroe/27c9bfe313331c583b6f235fcf4f7567 to your computer and use it in GitHub Desktop.
Save tormaroe/27c9bfe313331c583b6f235fcf4f7567 to your computer and use it in GitHub Desktop.
Project Euler problem #1 solved in APL
This is my very first APL program,
made from scratch using an APL primer at
http://aplwiki.com/LearnApl/BuiltInFunctions
Evaluated using http://tryapl.org/
Yields the sum of all multiples of 3 or 5 below 1000.
n999a+/n×0=3|nb+/n×0=5|nc+/n×0=15|na+b-c
Second version using a function definition..
f{+/×0=|}n999an f 3bn f 5cn f 15a+b-c
Third version using a closure, kind of..
n999f{+/n×0=|n}af 3bf 5cf 15a+b-c
..or a bit simpler without the temporary variables..
n999f{+/n×0=|n}(f 3)+(f 5)-(f 15)
..or maybe this is better?!
n999f{+/n×0=|n}(+/f¨ 3 5) - f 15
Inlining function is also possible :)
Doesn't make much sense in this case though.
n999(+/{+/n×0=|n}¨ 3 5) - {+/n×0=|n} 15
A variation using indexing..,
and now inlining the func makes more sense.
n999m{+/n×0=|n}¨ 3 5 15m[1]+m[2]-m[3]
Slightly more dense version of the latter..
n999m{+/n×0=|n}¨ 3 5 15(+/2m)-m[3]
Programming APL is actually quite fun \o/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment