Jensen's Device: Difference between revisions
Content added Content deleted
(→{{header|Forth}}: added version that passes i and 1/i as execution tokens) |
No edit summary |
||
Line 452: | Line 452: | ||
alert(sum(obj, 1, 100, function() {return 1 / obj.val}));</lang> |
alert(sum(obj, 1, 100, function() {return 1 / obj.val}));</lang> |
||
The alert shows us '5.187377517639621'. |
The alert shows us '5.187377517639621'. |
||
=={{header|Java}}== |
|||
This is Java 8. |
|||
<lang java>import java.util.function.*; |
|||
import java.util.stream.*; |
|||
public class Jensen2 { |
|||
static double sum(int lo, int hi, IntToDoubleFunction f) { |
|||
return IntStream.range(lo, hi).mapToDouble(f).sum(); |
|||
} |
|||
public static void main(String args[]) { |
|||
System.out.println(sum(1, 100, (i -> 1.0/i))); |
|||
} |
|||
} |
|||
</lang> |
|||
The program prints '5.17737751763962'. |
|||
=={{header|Joy}}== |
=={{header|Joy}}== |