Last active
November 18, 2015 08:29
-
-
Save prasincs/05fde7d1437820363256 to your computer and use it in GitHub Desktop.
Primes and Palidromes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns prime-days | |
(:require [clj-time.core :as t] | |
[clj-time.format :as f] | |
[clojure.pprint :refer [pprint]])) | |
(defn prime? [n] | |
(.isProbablePrime (BigInteger/valueOf n) 1)) | |
(defn formatted-date [n] | |
(f/unparse | |
(f/formatters :basic-date) | |
n)) | |
(defn date-time->num | |
[n] | |
(Integer/valueOf (formatted-date n))) | |
(defn prime-date? [d] | |
(prime? (date-time->num d))) | |
(defn palindrome-date? [d] | |
(let [f-date (f/unparse (f/formatter "MdYY") d)] | |
(= f-date (clojure.string/reverse f-date)))) | |
(defn get-all-days [pred? year] | |
(loop [d (t/now) | |
prime-days '() ] | |
(if (not (t/after? d (t/plus (t/now) (t/years (or year 10))))) | |
(recur (t/plus d (t/days 1)) | |
(if (pred? d) | |
(conj prime-days (f/unparse (f/formatters :date) d)) | |
prime-days)) | |
prime-days))) | |
(def get-all-primes | |
(partial get-all-days prime-date?)) | |
(def get-all-palidromes | |
(partial get-all-days palindrome-date?)) | |
(def get-prime-and-palindromes | |
(partial get-all-days (fn [d] | |
(and (prime-date? d) | |
(palindrome-date? d))))) | |
(def all-primes (get-all-primes 20)) | |
;; Primes and Palindrome days in the next 10 years | |
;; ("2021-12-09" "2021-12-03" "2019-09-13" "2018-08-01" "2016-06-11" "2016-06-01") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment