1 min read

APL Hacking: Project Euler (#19)

I didn't really have a good idea of how to solve this one until I started playing around with some things on the APL session. Actually, what do you call the APL REPL, anyways?

At any rate, it turned out to be a really easy solution once I realized how to get the Sundays and firsts out. I wanted to find out a way to do this without using membership, but membership seems to make it pretty easy, even if it is not the most efficient solution.

Actually, I was really surprised when I got the correct answer on the first try running this thing. :)

∇R←PENINETEEN;SUN;FIRST;LEAPFIRST;FIRSTS

⍝ How many Sundays fell on the first of the month
⍝ during the twentieth century?

⍝ Compute the Sundays of each year
⍝ We can drop the potential last Sunday because
⍝ we do not care abou it.
SUN←⊂2∘.+7×⍳52

⍝ Compute the Firsts of each year
FIRST←1,1++31 28 31 30 31 30 31 31 30 31 30
LEAPFIRST←1,1++31 29 31 30 31 30 31 31 30 31 30
FIRSTS←(FIRST LEAPFIRST)[⎕IO+0=4|⍳100]

R←+/,⊃FIRSTS∊¨SUN