[ create a new paste ] login | about

Link: http://codepad.org/oyZFRth9    [ raw code | output | fork ]

pbewig - Scheme, pasted on Feb 24:
; Mardi Gras

; Easter occurs each year on the first Sunday after the first full moon after
; the vernal equinox.  Mardi Gras occurs on the Tuesday of the seventh week
; before Easter.  This program calculates the date of Mardi Gras.

; The <A HREF="http://en.wikipedia.org/wiki/Computus">Computus</A> that
; determines the date of Easter was the most important mathematical
; calculation in the world for over a thousand years, and is still
; important today.  This computation is due to Jean Meeus:

(define (easter year)
  (let* ((a (modulo year 19))
         (b (quotient year 100))
         (c (modulo year 100))
         (d (quotient b 4))
         (e (modulo b 4))
         (f (quotient (+ b 8) 25))
         (g (quotient (+ (- b f) 1) 3))
         (h (modulo (- (+ (* 19 a) b 15) d g) 30))
         (i (quotient c 4))
         (k (modulo c 4))
         (l (modulo (- (+ 32 (* 2 e) (* 2 i)) h k) 7))
         (m (quotient (+ a (* 11 h) (* 22 l)) 451))
         (month (quotient (- (+ h l 114) (* 7 m)) 31))
         (day (+ (modulo (- (+ h l 114) (* 7 m)) 31) 1)))
    (values month day)))

; <A HREF="http://en.wikipedia.org/wiki/Julian_day">Date arithmetic</A>
; is provided by two library functions; in countries of the former British
; Empire, these functions are only valid for dates after 1753:

(define (julian year month day)
  (let* ((a (quotient (- 14 month) 12))
         (y (+ year 4800 (- a)))
         (m (+ month (* 12 a) -3)))
    (+ day
       (quotient (+ (* 153 m) 2) 5)
       (* 365 y)
       (quotient y 4)
       (- (quotient y 100))
       (quotient y 400)
       (- 32045))))

(define (gregorian julian)
  (let* ((j (+ julian 32044))
         (g (quotient j 146097))
         (dg (modulo j 146097))
         (c (quotient (* (+ (quotient dg 36524) 1) 3) 4))
         (dc (- dg (* c 36524)))
         (b (quotient dc 1461))
         (db (modulo dc 1461))
         (a (quotient (* (+ (quotient db 365) 1) 3) 4))
         (da (- db (* a 365)))
         (y (+ (* g 400) (* c 100) (* b 4) a))
         (m (- (quotient (+ (* da 5) 308) 153) 2))
         (d (+ da (- (quotient (* (+ m 4) 153) 5)) 122))
         (year (+ y (- 4800) (quotient (+ m 2) 12)))
         (month (+ (modulo (+ m 2) 12) 1))
         (day (+ d 1)))
    (values year month day)))

; Then the computation of Mardi Gras is simple:

(define (easter-plus year days)
  (call-with-values
    (lambda () (easter year))
    (lambda (month day)
      (gregorian
        (+ (julian year month day) days)))))

(define (mardi-gras year)
  (call-with-values
    (lambda () (easter-plus year -47))
    (lambda (year month day)
      (display month) (display "/")
      (display day) (display "/")
      (display year) (newline))))

; Mardi Gras occurs on February 24, 2009:

(mardi-gras 2009)


Output:
1
2/24/2009


Create a new paste based on this one


Comments: