Skip to content

Instantly share code, notes, and snippets.

@ihodes
Created August 20, 2010 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihodes/540928 to your computer and use it in GitHub Desktop.
Save ihodes/540928 to your computer and use it in GitHub Desktop.
(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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment