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' ref_i lo hi term =
let sum' i lo hi term =
let rec aux acc i =
let rec aux acc =
if i > hi then
if !i > hi then
acc
acc
else begin
else begin
ref_i := i;
incr i;
aux (acc +. term ()) (i+1)
aux (acc +. term ())
end
end
in
in
aux 0. lo
i := lo - 1;
aux 0.


let () =
let () =