Jensen's Device: Difference between revisions
Content deleted Content added
added c++ |
|||
Line 53: | Line 53: | ||
END</pre> |
END</pre> |
||
Output: +5.18737751763962e +0 |
Output: +5.18737751763962e +0 |
||
=={{header|C++}}== |
|||
<cpp>#include <iostream> |
|||
int i; |
|||
double sum(int &i, int lo, int hi, double (*term)()) { |
|||
double temp = 0; |
|||
for (i = lo; i <= hi; i++) |
|||
temp += term(); |
|||
return temp; |
|||
} |
|||
double term_func() { return 1.0 / i; } |
|||
int main () { |
|||
std::cout << sum(i, 1, 100, term_func) << std::endl; |
|||
return 0; |
|||
}</cpp> |
|||
Output: 5.18738 |
|||
=={{header|E}}== |
=={{header|E}}== |
||
Line 111: | Line 130: | ||
<ocaml>let i = ref 42 (* initial value doesn't matter *) |
<ocaml>let i = ref 42 (* initial value doesn't matter *) |
||
let sum' |
let sum' i lo hi term = |
||
let rec aux acc |
let rec aux acc = |
||
if i > hi then |
if !i > hi then |
||
acc |
acc |
||
else begin |
else begin |
||
incr i; |
|||
aux (acc +. term () |
aux (acc +. term ()) |
||
end |
end |
||
in |
in |
||
i := lo - 1; |
|||
aux 0. |
|||
let () = |
let () = |