March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,3 +1,2 @@
: s>f s>d d>f ;
: sum 0 s>f 1+ swap ?do i over execute f+ loop drop ;
:noname s>f 1 s>f fswap f/ ; 1 100 sum f.

View file

@ -0,0 +1,6 @@
fvariable ii \ i is a Forth word that we need
: sum ( xt1 lo hi xt2 -- r )
0e swap 1+ rot ?do ( addr xt r1 )
i s>f over execute f! dup execute f+
loop 2drop ;
' ii 1 100 :noname 1e ii f@ f/ ; sum f.

View file

@ -0,0 +1,12 @@
import java.util.function.*;
import java.util.stream.*;
public class Jensen {
static double sum(int lo, int hi, IntToDoubleFunction f) {
return IntStream.rangeClosed(lo, hi).mapToDouble(f).sum();
}
public static void main(String args[]) {
System.out.println(sum(1, 100, (i -> 1.0/i)));
}
}

View file

@ -0,0 +1,21 @@
public class Jensen2 {
interface IntToDoubleFunction {
double apply(int n);
}
static double sum(int lo, int hi, IntToDoubleFunction f) {
double res = 0;
for (int i = lo; i <= hi; i++)
res += f.apply(i);
return res;
}
public static void main(String args[]) {
System.out.println(
sum(1, 100,
new IntToDoubleFunction() {
public double apply(int i) { return 1.0/i;}
}));
}
}