Skip to content

Commit ee4c70d

Browse files
committed
Added first two Eueler problems in Java 8
1 parent b5ffcf4 commit ee4c70d

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

.idea/.name

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ apply plugin: 'application'
1414

1515
defaultTasks 'build'
1616

17-
mainClassName = "fj.demo.Array_forall"
17+
//mainClassName = "fj.demo.Array_forall"
18+
mainClassName = "fj.demo.euler.Problem2"
1819
//mainClassName = "fj.demo.Primes2"
1920

2021
ext {
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package fj.demo.euler;
22

33
import fj.F;
4+
import fj.data.Stream;
5+
46
import static fj.data.List.range;
57
import static fj.function.Integers.sum;
68

@@ -10,9 +12,19 @@
1012
* Add all the natural numbers below one thousand that are multiples of 3 or 5.
1113
*/
1214
public class Problem1 {
13-
public static void main(final String[] args) {
14-
out.println(sum(range(0, 1000).filter(new F<Integer, Boolean>() {
15-
public Boolean f(final Integer a) { return a % 3 == 0 || a % 5 == 0;}
16-
})));
17-
}
15+
public static void main(final String[] args) {
16+
java7();
17+
java8();
18+
}
19+
20+
public static void java7() {
21+
out.println(sum(range(0, 1000).filter(new F<Integer, Boolean>() {
22+
public Boolean f(final Integer a) { return a % 3 == 0 || a % 5 == 0;}
23+
})));
24+
}
25+
26+
public static void java8() {
27+
out.println(Stream.range(0, 1000).filter(i -> i % 3 == 0 || i % 5 == 0).foldLeft((acc, i) -> acc + i, 0));
28+
}
29+
1830
}

demo/src/main/java/fj/demo/euler/Problem2.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,26 @@
1313
* Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
1414
*/
1515
public class Problem2 {
16-
public static void main(final String[] args) {
17-
final Stream<Integer> fibs = new F2<Integer, Integer, Stream<Integer>>() {
18-
public Stream<Integer> f(final Integer a, final Integer b) {
19-
return cons(a, curry().f(b).lazy().f(a + b));
20-
}
21-
}.f(1, 2);
22-
out.println(sum(fibs.filter(even).takeWhile(intOrd.isLessThan(4000001)).toList()));
23-
}
16+
public static void main(final String[] args) {
17+
java7();
18+
java8();
19+
}
20+
21+
static void java7() {
22+
final Stream<Integer> fibs = new F2<Integer, Integer, Stream<Integer>>() {
23+
public Stream<Integer> f(final Integer a, final Integer b) {
24+
return cons(a, curry().f(b).lazy().f(a + b));
25+
}
26+
}.f(1, 2);
27+
out.println(sum(fibs.filter(even).takeWhile(intOrd.isLessThan(4000001)).toList()));
28+
}
29+
30+
static F2<Integer, Integer, Stream<Integer>> fibsJava8 = (a, b) -> {
31+
return cons(a, fibsJava8.curry().f(b).lazy().f(a + b));
32+
};
33+
34+
static void java8() {
35+
out.println(sum(fibsJava8.f(1, 2).filter(even).takeWhile(intOrd.isLessThan(4000001)).toList()));
36+
}
37+
2438
}
File renamed without changes.

0 commit comments

Comments
 (0)