-
Notifications
You must be signed in to change notification settings - Fork 0
/
75.clj
45 lines (38 loc) · 844 Bytes
/
75.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
43
44
45
(ns e75
(: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))))
(time (println
(count
(filter #(= (val %)
1)
(frequencies
(map #(nth % 3)
(triples 1500000)))))))