Skip to content

Instantly share code, notes, and snippets.

@jamiepratt
Last active July 26, 2022 08:47
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 jamiepratt/e4e3b58f16ef75b5cd35e5abc84f8351 to your computer and use it in GitHub Desktop.
Save jamiepratt/e4e3b58f16ef75b5cd35e5abc84f8351 to your computer and use it in GitHub Desktop.
(ns coin-toss
(:require [clojure.test :as t]))
(defn expt
"x to the yth power" [x y]
(apply * (repeat y x)))
(defn sum-of-geometric-series
"a(1-r^n )/(1-r)" [a r n]
(* a (/ (- 1 (expt r n)) (- 1 r))))
(t/deftest sum-of-geometric-series-test
(t/testing "geometric series with a=1/6, r=5/6 and n=4 should equal 1- ((5/6) ^ 4)"
(t/is (= (coin-toss/sum-of-geometric-series 1/6 5/6 4) (- 1 (coin-toss/expt 5/6 4))))
(t/is (= (coin-toss/sum-of-geometric-series 1/36 35/36 24) (- 1 (coin-toss/expt 35/36 24))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment