Last active
August 16, 2016 16:51
-
-
Save craftybones/635493ec41eab26ad9512c28d489e569 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 jitu-problem.core | |
(:require [clojure.math.numeric-tower :as math] | |
[clojure.set :as cset]) | |
(:gen-class)) | |
(defn divisible-by? | |
[x y] | |
(zero? (rem y x))) | |
(defn prime-candidates | |
([] (lazy-seq (list* 2 3 (prime-candidates 1)))) | |
([x] (lazy-seq (list* (dec (* x 6)) | |
(inc (* x 6)) | |
(prime-candidates (inc x)))))) | |
(def not-divisible-by? (comp not divisible-by?)) | |
(def closest-integer-sqrt (comp first math/exact-integer-sqrt)) | |
(def none (comp not some)) | |
(defn can-divide? | |
[x y] | |
(divisible-by? y x)) | |
(defn prime-candidates-less-than | |
[threshold] | |
(take-while (partial >= threshold) (prime-candidates))) | |
(defn is-prime? | |
[x] | |
(let [threshold (closest-integer-sqrt x) | |
x-divisible-by? (partial can-divide? x)] | |
(none x-divisible-by? (prime-candidates-less-than threshold)))) | |
(def primes (comp (partial filter is-prime?) prime-candidates)) | |
(defn prime-factors-of [x] | |
(let [x-divisible-by? (partial can-divide? x)] | |
(filter x-divisible-by? (prime-candidates-less-than x)))) | |
(defn equalise-a? [a xyz] | |
(let [pf (map (comp (partial into #{}) prime-factors-of) a) | |
common-pf (apply cset/intersection pf) | |
all-pf (apply cset/union pf) | |
unique-pf (cset/difference all-pf common-pf)] | |
(cset/subset? unique-pf xyz))) | |
(def can-she-equalise? | |
(comp {true "She can" false "She can't"} equalise-a?)) | |
(defn -main | |
[& args] | |
(println (can-she-equalise? #{2 4} #{2})) | |
(println (can-she-equalise? #{2 3 6 7} #{2 3})) | |
(println (can-she-equalise? #{49 98} #{2}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment