DEV Community

Discussion on: Daily Challenge #78 - Number of Proper Fractions with Denominator d

Collapse
 
larisho profile image
Gab

Clojure:

(defn get-divisors [num]
  "Returns sorted list of divisors"
  (for [x (range 1 num)
        :when (= (mod num x) 0)]
    x))

(defn can-reduce? [num denom]
  "Returns true if fraction can be reduced, otherwise false"
  (first
   (filter #(= (mod num %) 0) (rest (get-divisors denom)))))

(defn proper-fractions [denom]
  "Returns the number of proper fractions that 
can be created with the given denominator"
  (count
   (filter #(not (can-reduce? % denom)) (range 1 denom))))

(proper-fractions 1)
;; 0
(proper-fractions 2)
;; 1
(proper-fractions 5)
;; 5
(proper-fractions 15)
;; 8
(proper-fractions 25)
;; 20