Skip to content

Instantly share code, notes, and snippets.

@joshuacalloway
Created May 30, 2014 01:04
Show Gist options
  • Save joshuacalloway/b472efd10a4882c61053 to your computer and use it in GitHub Desktop.
Save joshuacalloway/b472efd10a4882c61053 to your computer and use it in GitHub Desktop.
euler problem 19
(ns euler.problem19)
(defn isleapyear?[yr]
(and (not (zero? (mod yr 100))) (= 0 (mod yr 4))))
(def nonleapmonths [31 28 31 30 31 30 31 31 30 31 30 31])
(def leapmonths [31 29 31 30 31 30 31 31 30 31 30 31])
(defn getfirstmonthdays [yr]
(loop [index 0
yr yr
firstdaysofmonths [1]]
(let [months (if (isleapyear? yr) leapmonths nonleapmonths) ]
(if (= 1188 index)
firstdaysofmonths
(recur (inc index) (+ yr (/ index 12)) (conj firstdaysofmonths (+ (peek firstdaysofmonths )(months (mod index 12)))))))))
(defn countsundaysonfirstonmonth [firstsunday firsdaysofmonth]
(count (filter #(or (= firstsunday %) (= firstsunday (mod % 7))) firsdaysofmonth)) )
(countsundaysonfirstonmonth 4 (getfirstmonthdays 1901))
(ns euler.problem19)
(defn isleapyear?[yr]
(and (not (zero? (mod yr 100))) (= 0 (mod yr 4))))
(def nonleapmonths [31 28 31 30 31 30 31 31 30 31 30 31])
(def leapmonths [31 29 31 30 31 30 31 31 30 31 30 31])
(defn getfirstmonthdays [yr]
(loop [index 0
yr yr
firstdaysofmonths [1]]
(let [months (if (isleapyear? yr) leapmonths nonleapmonths) ]
(if (= 1188 index)
firstdaysofmonths
(recur (inc index) (+ yr (/ index 12)) (conj firstdaysofmonths (+ (peek firstdaysofmonths )(months (mod index 12)))))))))
(defn countsundaysonfirstonmonth [firstsunday firsdaysofmonth]
(count (filter #(or (= firstsunday %) (= firstsunday (mod % 7))) firsdaysofmonth)) )
(countsundaysonfirstonmonth 4 (getfirstmonthdays 1901))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment