Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
17
Task/Pythagorean-triples/Clojure/pythagorean-triples-1.clj
Normal file
17
Task/Pythagorean-triples/Clojure/pythagorean-triples-1.clj
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defn gcd [a b] (if (zero? b) a (recur b (mod a b))))
|
||||
|
||||
(defn pyth [peri]
|
||||
(for [m (range 2 (Math/sqrt (/ peri 2)))
|
||||
n (range (inc (mod m 2)) m 2) ; n<m, opposite polarity
|
||||
:let [p (* 2 m (+ m n))] ; = a+b+c for this (m,n)
|
||||
:while (<= p peri)
|
||||
:when (= 1 (gcd m n))
|
||||
:let [m2 (* m m), n2 (* n n),
|
||||
[a b] (sort [(- m2 n2) (* 2 m n)]), c (+ m2 n2)]
|
||||
k (range 1 (inc (quot peri p)))]
|
||||
[(= k 1) (* k a) (* k b) (* k c)]))
|
||||
|
||||
(defn rcount [ts] ; (->> peri pyth rcount) produces [total, primitive] counts
|
||||
(reduce (fn [[total prims] t] [(inc total), (if (first t) (inc prims) prims)])
|
||||
[0 0]
|
||||
ts))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(defn pyth-count [peri]
|
||||
(reduce (fn [[total prims] k] [(+ total k), (inc prims)]) [0 0]
|
||||
(for [m (range 2 (Math/sqrt (/ peri 2)))
|
||||
n (range (inc (mod m 2)) m 2) ; n<m, opposite polarity
|
||||
:let [p (* 2 m (+ m n))] ; = a+b+c for this (m,n)
|
||||
:while (<= p peri)
|
||||
:when (= 1 (gcd m n))]
|
||||
(quot peri p))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue