File tree Expand file tree Collapse file tree
exercises/practice/perfect-numbers/.meta Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22 (:require [clojure.math :as math]))
33
44(defn prime-factors-of
5- [n ]
5+ [num ]
66 (loop [result []
77 candidate 2
8- n n ]
8+ n num ]
99 (cond
1010 (= n 1 ) (frequencies result)
1111 (> candidate (math/sqrt n)) (recur (conj result n) candidate 1 )
1212 (zero? (mod n candidate)) (recur (conj result candidate) candidate (quot n candidate))
1313 :else (recur result (inc candidate) n))))
1414
15- (defn generate-factor- powers
16- [factor max-power]
15+ (defn powers-of
16+ [num max-power]
1717 (for [power (range (inc max-power))]
18- (reduce * (repeat power factor ))))
18+ (reduce * (repeat power num ))))
1919
2020(defn cartesian-product
2121 [& collections]
2525 (* a b)))
2626 [1 ] collections))
2727
28- (defn generate-factors
29- [n]
30- (let [prime-factors (prime-factors-of n)]
31- (->> prime-factors
32- (or (seq prime-factors) [])
33- (map #(generate-factor-powers (key %) (val %)))
34- (apply cartesian-product))))
28+ (defn factors-of
29+ [num]
30+ (->> (prime-factors-of num)
31+ (map #(powers-of (first %) (second %)))
32+ (apply cartesian-product)))
33+
34+ (defn aliquot-sum
35+ [num]
36+ (reduce + (disj (set (factors-of num)) num)))
3537
3638(defn classify
37- [n ]
38- (let [sum (reduce + ( disj ( set ( generate-factors n)) n) )]
39+ [num ]
40+ (let [sum (aliquot-sum num )]
3941 (cond
40- (> sum n ) :abundant
41- (= sum n ) :perfect
42- (< sum n ) :deficient )))
42+ (> sum num ) :abundant
43+ (= sum num ) :perfect
44+ (< sum num ) :deficient )))
You can’t perform that action at this time.
0 commit comments