
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
(defun is-leap-year (year) (or (and (zerop (mod year 4)) (not (zerop (mod year 100)))) (zerop (mod year 400)))) (defun days-in-month (month year) (case month ((1 3 5 7 8 10 12) 31) ((4 6 9 11) 30) (2 (if (is-leap-year year) 29 28)))) (defun euler-19 () (let ((day-of-week 2) ; 1 Jan 1901 was a Tuesday (sundays 0)) (loop :for year :from 1901 :to 2000 :do (loop :for month :from 1 :to 12 :do (when (zerop day-of-week) ; 0 represents Sunday (incf sundays)) (setf day-of-week (mod (+ day-of-week (days-in-month month year)) 7)))) sundays)) (print (euler-19))
CL-USER> (euler-19) 171 CL-USER>
— Me@2024-01-30 01:46:11 PM
.
.
2024.01.30 Tuesday (c) All rights reserved by ACHK



You must be logged in to post a comment.