-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler23.clj
40 lines (27 loc) · 948 Bytes
/
euler23.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
(ns euler.twentythree
(:gen-class)
(:require [clojure.math.combinatorics :as combo]
[clojure.core.reducers :as r]
[clojure.set :as s]))
(defn is-divisor? [n m]
(= 0 (mod n m)))
(defn get-upper-bound-divisor [n] (+ 1 (int (/ n 2))))
(defn collect-divisors [n]
(let [nums (range 1 (get-upper-bound-divisor n))]
(->> nums
(r/filter #(is-divisor? n %)))))
(defn find-sum-of-divs [n]
(r/fold + (collect-divisors n)))
(defn is-abundant? [n] (> (find-sum-of-divs n) n))
(defn get-abundant-numbers [n]
(r/foldcat (r/filter is-abundant? (range 1 n))))
(def max-domain-value 28124)
(def abundant-numbers (get-abundant-numbers max-domain-value))
(def abundant-sums
(->> (combo/cartesian-product abundant-numbers abundant-numbers)
(r/map #(+ (first %) (last %)))
(r/foldcat)))
(reduce +
(s/difference
(set (range 1 max-domain-value))
(set abundant-sums)))