Section 7.5. Finding the Nth Weekday in a Month


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      # Find month/day for 2001 date = [2001] + date    # Tack year on front t = Time.local *date    # Pass parameters to Time.local puts t                  # Sun Apr 15 01:00:00 GMT-8:00 2001


One reader on seeing this section on Easter, asked: "Ecclesiastical or astronomical?" Truthfully, I don't know. If you find out, let us all know.

I'd love to explain this algorithm to you, but I don't understand it myself. Some things must be taken on faith, and in the case of Easter, this may be especially appropriate.




The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net