Last active
August 29, 2015 14:16
-
-
Save zaneli/e8ac3fad802a8a2743b3 to your computer and use it in GitHub Desktop.
「ClojureでNinety-Nine Lisp Problems(P34~36)」ブログ用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(declare my-coprime? my-gcd) | |
(defn my-totient-phi | |
"Calculate Euler's totient function phi(m)." | |
[m] | |
(count (filter #(my-coprime? m %) (range 1 m)))) | |
(defn my-coprime? | |
"Determine whether two positive integer numbers are coprime." | |
[n m] | |
(let [g (my-gcd n m)] (or (== g 1) (== g -1)))) | |
(defn my-gcd | |
"Determine the greatest common divisor of two positive integer numbers." | |
[n m] | |
(cond | |
(== n 0) m | |
(neg? n) (my-gcd (- n) m) | |
(neg? m) (- (my-gcd n (- m))) | |
:else (my-gcd (rem m n) n))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn my-prime-factors | |
"Determine the prime factors of a given positive integer." | |
[n] | |
(loop [n n, m 2, ps nil] | |
(if (> (Math/pow m 2) n) | |
(reverse (cons n ps)) | |
(let [q (quot n m), r (rem n m)] | |
(if (== r 0) | |
(recur q m (cons m ps)) | |
(recur n (if (== m 2) (+ m 1) (+ m 2)) ps)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(declare count-quot) | |
(defn my-prime-factors | |
"Determine the prime factors of a given positive integer." | |
[n] | |
(loop [n n, m 2, ps nil] | |
(if (> (Math/pow m 2) n) | |
(let [ps' (if (== n 1) ps (cons n ps))] | |
(reverse ps')) | |
(let [[q cnt] (count-quot n m), | |
m' (if (== m 2) (+ m 1) (+ m 2)), | |
ps' (if (== cnt 0) ps (into ps (repeat cnt m)))] | |
(recur q m' ps'))))) | |
(defn count-quot | |
[n m] | |
(loop [n n, m m, cnt 0] | |
(let [q (quot n m), r (rem n m)] | |
(if (== r 0) | |
(recur q m (inc cnt)) | |
(list n cnt))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(declare count-quot) | |
(defn my-prime-factors-mult | |
"Determine the prime factors of a given positive integer (2)." | |
[n] | |
(loop [n n, m 2, ps nil] | |
(if (> (Math/pow m 2) n) | |
(let [ps' (if (== n 1) ps (cons (list n 1) ps))] | |
(reverse ps')) | |
(let [[q cnt] (count-quot n m), | |
m' (if (== m 2) (+ m 1) (+ m 2)), | |
ps' (if (== cnt 0) ps (cons (list m cnt) ps))] | |
(recur q m' ps'))))) | |
(defn count-quot | |
[n m] | |
(loop [n n, m m, cnt 0] | |
(let [q (quot n m), r (rem n m)] | |
(if (== r 0) | |
(recur q m (inc cnt)) | |
(list n cnt))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment