Created
November 13, 2013 20:42
-
-
Save siscia/7456061 to your computer and use it in GitHub Desktop.
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 solution | |
(:gen-class)) | |
(defn make-fibonaccis [a b] | |
(lazy-seq | |
(cons a (make-fibonaccis b (+ a b))))) | |
(defn make-primes [numbers] | |
(cons (first numbers) | |
(lazy-seq | |
(make-primes | |
(filter #(not= 0 (mod % (first numbers))) | |
(rest numbers)))))) | |
(def fibonaccis (make-fibonaccis 1 1)) | |
(def primes (make-primes (drop 2 (range)))) | |
(defn number-to-factors [n] | |
(loop [factors [] | |
n n | |
primes primes] | |
(if (== n 1) | |
(set factors) | |
(cond | |
(== 0 (mod n (first primes))) (recur (conj factors (first primes)) | |
(/ n (first primes)) | |
primes) | |
:default (recur factors n (next primes)))))) | |
(def divisor-of-fibonacci | |
(map | |
(fn [n] [n (number-to-factors n)]) | |
fibonaccis)) | |
(defn solve [n] | |
(let [factors-n (number-to-factors n)] | |
(->> (map (fn [[fib factors]] | |
(when-let [common-factor (some factors-n factors)] | |
[fib common-factor])) | |
divisor-of-fibonacci) | |
(remove nil?) | |
first))) | |
(defn solution [] | |
(let [in (clojure.java.io/reader *in*) | |
out (clojure.java.io/writer *out*)] | |
(for [_ (range (Integer/parseInt (.readLine in)))] | |
(apply print (solve (Integer/parseInt (.readLine in))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment