Skip to content

Instantly share code, notes, and snippets.

@kana-ph
Last active January 19, 2017 08:17
Show Gist options
  • Save kana-ph/6b70ffedb75a8438a5a1204b506a7a0b to your computer and use it in GitHub Desktop.
Save kana-ph/6b70ffedb75a8438a5a1204b506a7a0b to your computer and use it in GitHub Desktop.
Solution to https://projecteuler.net/problem=2 using Java8 streams/optionals
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