Last active
December 12, 2019 09:22
-
-
Save tormaroe/27c9bfe313331c583b6f235fcf4f7567 to your computer and use it in GitHub Desktop.
Project Euler problem #1 solved in APL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⍝ 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. | |
n←⍳999⋄a←+/n×0=3|n⋄b←+/n×0=5|n⋄c←+/n×0=15|n⋄a+b-c | |
⍝ Second version using a function definition.. | |
f←{+/⍺×0=⍵|⍺}⋄n←⍳999⋄a←n f 3⋄b←n f 5⋄c←n f 15⋄a+b-c | |
⍝ Third version using a closure, kind of.. | |
n←⍳999⋄f←{+/n×0=⍵|n}⋄a←f 3⋄b←f 5⋄c←f 15⋄a+b-c | |
⍝ ..or a bit simpler without the temporary variables.. | |
n←⍳999⋄f←{+/n×0=⍵|n}⋄(f 3)+(f 5)-(f 15) | |
⍝ ..or maybe this is better?! | |
n←⍳999⋄f←{+/n×0=⍵|n}⋄(+/f¨ 3 5) - f 15 | |
⍝ Inlining function is also possible :) | |
⍝ Doesn't make much sense in this case though. | |
n←⍳999⋄(+/{+/n×0=⍵|n}¨ 3 5) - {+/n×0=⍵|n} 15 | |
⍝ A variation using indexing.., | |
⍝ and now inlining the func makes more sense. | |
n←⍳999⋄m←{+/n×0=⍵|n}¨ 3 5 15⋄m[1]+m[2]-m[3] | |
⍝ Slightly more dense version of the latter.. | |
n←⍳999⋄m←{+/n×0=⍵|n}¨ 3 5 15⋄(+/2↑m)-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