aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2022-01-22 17:56:06 -0800
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2022-01-22 17:56:06 -0800
commit2103e2af70b2670555eacbf9f4a78613eda3c5b6 (patch)
tree4a5340a1c4bf4f50338863117166ca0346af35eb
parent943327d71a4e773266e54f88289b52d3624fefcf (diff)
downloadperlweeklychallenge-club-2103e2af70b2670555eacbf9f4a78613eda3c5b6.tar.gz
perlweeklychallenge-club-2103e2af70b2670555eacbf9f4a78613eda3c5b6.tar.bz2
perlweeklychallenge-club-2103e2af70b2670555eacbf9f4a78613eda3c5b6.zip
Ch148 (Clojure): Task #2
-rw-r--r--challenge-148/tyler-wardhaugh/clojure/src/c148/t2.clj30
-rw-r--r--challenge-148/tyler-wardhaugh/clojure/test/c148/t2_test.clj14
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-148/tyler-wardhaugh/clojure/src/c148/t2.clj b/challenge-148/tyler-wardhaugh/clojure/src/c148/t2.clj
new file mode 100644
index 0000000000..146d377ebc
--- /dev/null
+++ b/challenge-148/tyler-wardhaugh/clojure/src/c148/t2.clj
@@ -0,0 +1,30 @@
+(ns c148.t2
+ (:require [clojure.math :as m]
+ [clojure.pprint :refer [cl-format]]))
+
+;(set! *warn-on-reflection* true)
+(def DEFAULT-EPS 1e-6)
+(def MAX 100)
+
+(defn one-ish?
+ ([n] (one-ish? n DEFAULT-EPS))
+ ([n eps] (< (abs (- 1.0 n)) eps)))
+
+; equivalent formula, found at: https://math.stackexchange.com/a/1885139
+(defn cardano-triplet?
+ [a b c]
+ (one-ish? (- (+ (* 8 (m/pow a 3))
+ (* 15 (m/pow a 2))
+ (* 6 a))
+ (* 27 (m/pow b 2) c))))
+
+(def cardano-triplets
+ (for [a (range MAX)
+ b (range MAX)
+ c (range MAX)
+ :when (cardano-triplet? a b c)]
+ [a b c]))
+
+(defn -main
+ [& _]
+ (cl-format true "~{(~{~a~^, ~})~%~}" (take 5 cardano-triplets)))
diff --git a/challenge-148/tyler-wardhaugh/clojure/test/c148/t2_test.clj b/challenge-148/tyler-wardhaugh/clojure/test/c148/t2_test.clj
new file mode 100644
index 0000000000..37830d6b3e
--- /dev/null
+++ b/challenge-148/tyler-wardhaugh/clojure/test/c148/t2_test.clj
@@ -0,0 +1,14 @@
+(ns c148.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c148.t2 :refer [cardano-triplets]]))
+
+(def FIRST-FIVE-CARDANO-TRIPLETS
+ [[2 1 5]
+ [5 1 52]
+ [5 2 13]
+ [8 3 21]
+ [11 4 29]])
+
+(deftest target
+ (testing "Target identified in task description"
+ (is (= FIRST-FIVE-CARDANO-TRIPLETS (take 5 cardano-triplets)))))