## Thank God It’s Friday!

### June 24, 2011

The function for Gauss’ method is shown below. We changed the decimals to fractions so that all the calculations would be done in exact arithmetic.

`(define (day-of-week year month day) ; gauss method`

(let* ((yr (if (< month 3) (- year 1) year))

(m (if (< month 3) (+ month 10) (- month 2)))

(y (modulo yr 100)) (c (quotient yr 100)))

(list-ref '(sun mon tue wed thu fri sat)

(modulo (+ day (floor (- (* 13/5 m) 1/5)) y

(quotient y 4) (quotient c 4) (- (* 2 c))) 7))))

`> (day-of-week 2011 6 24)`

fri

Here is our version of Sakamoto’s method:

`(define (day-of-week year month day) ; sakamoto method`

(let ((t (vector 0 3 2 5 0 3 5 1 4 6 2 4))

(y (if (< month 3) (- year 1) year)))

(list-ref '(sun mon tue wed thu fri sat)

(modulo (+ day y (quotient y 4) (- (quotient y 100))

(quotient y 400) (vector-ref t (- month 1))) 7))))

`> (day-of-week 2011 6 24)`

fri

Conway’s method is more of a challenge. Here is the calculation of the anchor day:

`(define (anchor year)`

(let ((c (+ (quotient year 100) 1)))

(list-ref '(sun mon tue wed thu fri sat)

(modulo (+ (* 5 c) (quotient (- c 1) 4) 4) 7))))

`> (anchor 2011)`

tue

Given an anchor day, here is the function to calculate the doomsday:

`(define (doomsday year)`

(let* ((days '(sun mon tue wed thu fri sat))

(y (modulo year 100))

(q (quotient y 12))

(r (remainder y 12))

(x (quotient r 4))

(c (+ (quotient year 100) 1))

(anchor (+ (* 5 c) (quotient (- c 1) 4) 4)))

(list-ref days (modulo (+ q r x anchor) 7))))

`> (doomsday 2011)`

mon

It is easier for a computer, though perhaps not for a mentalist, to calculate the doomsday like this:

`(define (doomsday year)`

(list-ref '(sun mon tue wed thu fri sat)

(modulo (+ 2 year (quotient year 4)

(- (quotient year 100)) (quotient year 400)) 7)))

`> (doomsday 2011)`

mon

You can run the program at http://programmingpraxis.codepad.org/BeTpy03N, which also includes the code for the method from the Standard Prelude and Zeller’s congruence for comparison. Note that Zeller’s congruence is essentially the same as Gauss’ method.

Pages: 1 2

[…] today’s Programming Praxis exercise, our goal is to implement three functions related to dates: two ways to […]

My Haskell solution (see http://bonsaicode.wordpress.com/2011/06/24/programming-praxis-thank-god-it%E2%80%99s-friday/ for a version with comments):

My Python submission (basically just a translation of the Scheme solution):

Python version

Tried all the formulas from Wikipedia for conway’s method. And went all the way to calculate the day of the week.

Here’s my solution in Java: