Last active
July 26, 2022 08:47
-
-
Save jamiepratt/e4e3b58f16ef75b5cd35e5abc84f8351 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
(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