-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path139.clj
45 lines (36 loc) · 820 Bytes
/
139.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(ns e139
(:use clojure.contrib.math))
(defn coprime? [a b]
(= (gcd a b)
1))
(defn square [a]
(* a a))
(defn pqs [max-perimeter]
(let [upto (sqrt max-perimeter)]
(for [p (range (inc upto))
q (range 1 p)
:when (and (or (even? p) (even? q))
(coprime? p q))]
[p q])))
(defn primative [max-perimeter]
(for [[p q] (pqs max-perimeter)
:let [sqp (square p)
sqq (square q)
a (* 2 p q)
b (- sqp sqq)
c (+ sqp sqq)
per (+ (* 2 p q)
(* 2 sqp))]
]
[a b c per]))
(defn triples [max-perimeter]
(for [t (primative max-perimeter)
mult (iterate inc 1)
:while (< (* (nth t 3) mult)
max-perimeter)]
(vec (map #(* mult %) t))))
(println
(count
(for [[a b c per] (triples 100000000)
:when (= 0 (abs (- b a)))]
[a b c]))