Created
April 3, 2012 01:04
-
-
Save ponkore/2288478 to your computer and use it in GitHub Desktop.
ユークリッド互除法による最大公約数
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
;; mod を普通に使う | |
(defn gcd [m n] | |
(if (zero? n) m | |
(gcd n (mod m n)))) | |
(gcd 1763 1927) | |
; => 41 |
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
;; mod を使わず引き算だけで求める | |
(defn gcd [a b] | |
(cond (= a b) a | |
(> a b) (gcd (- a b) b) | |
:else (gcd a (- b a)))) | |
(gcd 1763 1927) | |
; => 41 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment