Skip to content

Instantly share code, notes, and snippets.

@lagenorhynque
Last active October 1, 2017 18:07
Show Gist options
  • Select an option

  • Save lagenorhynque/f1a9389f0e2d2b999a3e5f20458e8b20 to your computer and use it in GitHub Desktop.

Select an option

Save lagenorhynque/f1a9389f0e2d2b999a3e5f20458e8b20 to your computer and use it in GitHub Desktop.
user> (require '[clojure.math.numeric-tower :as math])
nil
;; y = e^xの区間[0,1]の面積の近似
user> (->> (map *
(repeat 1/1000)
(map #(Math/exp %) (range 0 1 1/1000)))
(apply +))
1.7174228307349653
;; y = x^2の区間[0,1]の面積の近似
user> (->> (map *
(repeat 1/1000)
(map #(* % %) (range 0 1 1/1000)))
(apply +)
double)
0.3328335
user>
λ> import Data.Ratio ((%))
-- y = e^xの区間[0,1]の面積の近似
λ> sum $ zipWith (*) (repeat 0.001) (map (\x -> exp . realToFrac $ x) [0, 1%1000..999%1000])
1.7174228307349653
-- y = x^2の区間[0,1]の面積の近似
λ> realToFrac . sum $ zipWith (*) (repeat $ 1%1000) (map (\x -> x * x) [0, 1%1000..999%1000])
0.3328335
λ>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment