Last active
January 19, 2017 08:17
-
-
Save kana-ph/6b70ffedb75a8438a5a1204b506a7a0b to your computer and use it in GitHub Desktop.
Solution to https://projecteuler.net/problem=2 using Java8 streams/optionals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.stream.Stream; | |
public class Fibo { | |
public static void main(String[] args) { | |
Arrays | |
.stream(args) | |
.findFirst() | |
.map(Long::parseLong) | |
.map(Fibo::computeFiboSum) | |
.ifPresent(System.out::println); | |
} | |
private static long computeFiboSum(long limit) { | |
return Stream | |
.iterate(new long[] {1, 2}, f -> new long[] {f[1], f[0]+f[1]}) | |
.limit(90) | |
.mapToLong(f -> f[0]) | |
.filter(n -> n < limit) | |
.filter(n -> n % 2 == 0) | |
.sum(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment