Avi Drucker / May 02 2021
Remix of Clojure by Nextjournal
Is Leap Year Jam
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
;; complient is used for autocompletion
;; add your libs here (and restart the runtime to pick up changes)
compliment/compliment {:mvn/version "0.3.9"}
tolitius/calip {:mvn/version "0.1.5"}
}}
deps.edn
Extensible Data Notation
{:hello (clojure-version)}
0.1s
Clojure
In general terms the algorithm for calculating a leap year is as follows...
A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.
Source: https://stackoverflow.com/questions/725098/leap-year-calculation
(defn x-divisible-by-y [x y]
(zero? (mod x y)))
0.2s
Clojure
(defn leap-year? [year]
(cond
(and (x-divisible-by-y year 4) (x-divisible-by-y year 100) (x-divisible-by-y year 400)) true
(and (x-divisible-by-y year 4) (not (x-divisible-by-y year 100))) true
:else false))
0.1s
Clojure
(defn leap-year-fast? [year]
(if (x-divisible-by-y year 4)
(if (not (x-divisible-by-y year 100))
true
(x-divisible-by-y year 400))
false))
0.0s
Clojure
(= true (leap-year? 1996) (leap-year? 1992) (leap-year? 1988) (leap-year? 2000))
0.0s
Clojure
(= false (leap-year? 1900) (leap-year? 1800) (leap-year? 1700))
0.0s
Clojure
(= true (leap-year-fast? 1996) (leap-year-fast? 1992) (leap-year-fast? 1988) (leap-year-fast? 2000))
0.0s
Clojure
(= false (leap-year-fast? 1900) (leap-year-fast? 1800) (leap-year-fast? 1700))
0.0s
Clojure
(require [calip.core :as calip])
0.5s
Clojure
(calip/measure {user/leap-year?
user/leap-year-fast?})
0.3s
Clojure
(= true (leap-year? 1996) (leap-year? 1992) (leap-year? 1988) (leap-year? 2000))
(= false (leap-year? 1900) (leap-year? 1800) (leap-year? 1700))
(= true (leap-year-fast? 1996) (leap-year-fast? 1992) (leap-year-fast? 1988) (leap-year-fast? 2000))
(= false (leap-year-fast? 1900) (leap-year-fast? 1800) (leap-year-fast? 1700))
0.3s
Clojure