Created
November 22, 2016 02:11
-
-
Save nvbn/42fe4dfb641d094b9848ea5f2f273c82 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 math-6.core | |
(:require [clojure.math.numeric-tower :refer [abs]])) | |
(def boundaries (range -100 100)) | |
(defn- get-cs-after-second | |
[c-before-before c-before] | |
(when-let [c (mod c-before-before c-before)] | |
(if (zero? c) | |
[0] | |
(lazy-seq (cons c (get-cs-after-second c-before c)))))) | |
(defn get-cs | |
[A B] | |
(concat [A B] (get-cs-after-second A B))) | |
(defn- get-k | |
[cs i c] | |
(if (< i 3) | |
1 | |
(/ (- (nth cs (- i 2)) c) | |
(nth cs (dec i))))) | |
(defn get-ks | |
[cs] | |
(map-indexed #(get-k cs %1 %2) cs)) | |
(defn- find-a-b | |
[A B c] | |
(->> (for [a boundaries | |
b boundaries | |
:when (= c (+ (* a A) (* b B)))] | |
[a b]) | |
(sort-by #(abs (first %))) | |
first)) | |
(defn get-a-bs | |
[cs] | |
(map-indexed #(cond | |
(zero? %1) [1 0] | |
(= 1 %1) [0 1] | |
:else (find-a-b (first cs) (second cs) %2)) | |
cs)) | |
(defn get-table | |
[A B] | |
(let [cs (get-cs A B) | |
ks (get-ks cs) | |
a-bs (get-a-bs cs)] | |
(map #(apply vector %1 %2 %3) cs ks a-bs))) | |
(defn get-expected-table | |
[] | |
(->> (for [A (range 2 100) | |
B (range 1 A)] | |
(get-table A B)) | |
(filter #(= (count %) 7)) | |
first)) | |
(defn print-table! | |
[table] | |
(println "c\tk_i\ta_i\tb_i\t") | |
(doseq [[c k a b] table] | |
(println c "\t" k "\t" a "\t" b))) | |
(print-table! (get-expected-table)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment