Skip to content

Commit

Permalink
Add q74 and q75
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Sep 19, 2024
1 parent 20e140d commit e712f18
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/ex/q074.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
;; https://4clojure.oxal.org/#/problem/074

(ns ex.q074)

(defn ans
"Problem 74, Filter Perfect Squares"
[s]
(letfn [(square? [x]
(cond
(< x 1) false
(let [y (int (Math/sqrt x))] (= (* y y) x)) true))]
(->> s
(re-seq #"\d+")
(map #(Integer/parseInt %))
(filter square?)
(interpose ",")
(apply str))))
13 changes: 13 additions & 0 deletions src/ex/q075.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;; https://4clojure.oxal.org/#/problem/075

(ns ex.q075)

(defn ans
"Problem 75, Euler's Totient Function"
[x]
(letfn [(gcd [x y]
(loop [x x y y]
(if (= x y) x (recur (- (max x y) (min x y)) (min x y)))))
(coprime? [x y] (= 1 (gcd x y)))
(coprimes [y] (filter #(coprime? y %) (range 1 y)))]
(if (< x 2) 1 (count (coprimes x)))))
11 changes: 11 additions & 0 deletions test/ex/q074_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;; https://4clojure.oxal.org/#/problem/074

(ns ex.q074-test
(:require [clojure.test :as t]
[ex.q074 :as sut]))

(t/deftest test-q74
(t/testing "Problem 74, Filter Perfect Squares"
(t/is (= "4,9" (sut/ans "4,5,6,7,8,9")))
(t/is (= "16,25,36" (sut/ans "15,16,25,36,37")))
(t/is (= "1,9,100" (sut/ans "1,2,5,6,9,11,20,30,100,101")))))
12 changes: 12 additions & 0 deletions test/ex/q075_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
;; https://4clojure.oxal.org/#/problem/075

(ns ex.q075-test
(:require [clojure.test :as t]
[ex.q075 :as sut]))

(t/deftest test-q75
(t/testing "Problem 75, Euler's Totient Function"
(t/is (= 1 (sut/ans 1)))
(t/is (= (count '(1 3 7 9)) 4 (sut/ans 10)))
(t/is (= 16 (sut/ans 40)))
(t/is (= 60 (sut/ans 99)))))

0 comments on commit e712f18

Please sign in to comment.