Created
August 20, 2010 19:03
-
-
Save ihodes/540928 to your computer and use it in GitHub Desktop.
This file contains 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 sum | |
[& s] | |
(reduce + s)) | |
(defn prod | |
[& s] | |
(reduce * s)) | |
(defn sigma | |
[s f] | |
(apply sum (map f s))) | |
(defn pi | |
[s f] | |
(apply prod (map f s))) | |
(defn generalized-continued-fraction | |
"Returns the value of the GCF of the sequences of numerators | |
and denominators. 'denomseq must have one more element than | |
'numseq." | |
[numseq denomseq] | |
(let [sn (reverse numseq) sd (reverse denomseq) ] | |
(loop [hn (first sn) sn (rest sn) hd (first sd) sd (rest sd)] | |
(cond (empty? sd) hd | |
:else (recur (first sn) (rest sn) | |
(+ (first sd) (/ hn hd)) (rest sd)))))) | |
(defn simple-continued-fraction | |
"Returns the final value of the SCF of 'sequence." | |
[sequence] | |
(generalized-continued-fraction | |
(take (dec (count sequence)) (repeat 1)) sequence)) |
This file contains 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment