Created
October 17, 2014 01:00
-
-
Save dpzmick/b2114287a9b18f416ed3 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
let rec read_inp () = | |
try | |
let i = read_float() in | |
i::(read_inp ()) | |
with End_of_file -> [] | |
;; | |
(* todo tail recursive *) | |
let rec fact = function | |
| 1 -> 1 | |
| n -> n * (fact (n-1)) | |
;; | |
(* creates a function f(x) that approximates e^x to n terms *) | |
let rec series = function | |
| 0 -> (fun x -> 1.0) | |
| n -> (fun x -> | |
let my_term = x ** (float n) /. (float (fact n)) in | |
(my_term +. ((series (n-1)) x))) | |
;; | |
(* make function that will approx to 10 terms (0..9) *) | |
let expansion = series 9;; | |
let _ = ignore (read_int ()); | |
List.iter | |
(fun f -> Printf.printf "%.4f" (expansion f); print_newline ();) | |
(read_inp ());; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment