7.4. Determining the Date of Easter

Traditionally this holiday is one of the most difficult to compute because it is tied to the lunar cycle. The lunar month does not go evenly into the solar year, and thus anything based on the moon can be expected to vary from year to year.

The algorithm we present here is a well-known one that has made the rounds. We have seen it coded in BASIC, Pascal, and C. We now present it to you in Ruby:

def easter(year) c = year/100 n = year - 19*(year/19) k = (c-17)/25 i = c - c/4 - (c-k)/3 + 19*n + 15 i = i - 30*(i/30) i = i - (i/28)*(1 -(i/28)*(29/(i+1))*((21-n)/11)) j = year + year/4 + i + 2 - c + c/4 j = j - 7*(j/7) l = i - j month = 3 + (l+40)/44 day = l + 28 - 31*(month/4) [month, day] end date = easter 2001 ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.