“By relieving the brain of all unnecesary work, a good notation sets it free to concentrate on more advanced problems, and in effect increases the mental power of the race.” – A. N. Whitehead
Yesterday Andreas posted this awesome video of a Game of Life coding session in APL, which really blew my mind. Watch it, it’s well worth 8 minutes of your time!
Being extremely impressed by what I saw, I realized that I never wrote anything in a language from the APL family, so I decided to change that. I couldn’t find a free version of APL running on OSX that wasn’t time limited or required me to apply for an educational license, so I finally settled on J (Wikipedia article), skimmed the docs and wrote solutions to 4 different Project Euler problems. They are all extremely terse, but I lack the APL/J knowledge to further comment on their quality. If you have experience in either language, I’d really appreciate some comments!
Problem 7: Find the 10001st prime.
This ones fairly easy, since the vocabulary contains p: which will return the n-th prime (0 indexed):
1
|
|
Problem 3: What is the largest prime factor of the number 600851475143?
1
|
|
Get the tail ({:) of the list of prime factors (q:)
Problem 5: What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
1
|
|
Behead (}.) the list of all non-negative integers up to 20 (i.20) and apply (/) the least common multiple (*.).
Problem 1: Find the sum of all the multiples of 3 or 5 below 1000.
This one took me a bit, so we’ll do it step by step. First, apply (/) the residue (|) to the non-negative integers up to (non-inclusive) 1000 (i.1000):
1 2 3 |
|
Filter out the elements with index 0 (0 ([: I. =)):
1 2 3 |
|
Finally append the two lists (,), remove duplicates (~.) and apply (/) +:
1
|
|
Overall playing with my first non-von Neumann programming language was a rather fun experience, especially after getting over the somewhat strange syntax and the fact that most “words” have monadic and dyadic versions. From my cursory glances J’s documentation looks pretty solid and the language starts making sense a lot quicker than one might expect. It’s definitely stimulating to play around with and I think I’ll do a bit more of that over the next few weeks, especially since I haven’t yet explored the plotting and OpenGL frameworks.