Saturday, May 10, 2014

When Java 8 meets Project Euler

Here is a Java implementation of a Project Euler problem. Used lambda expression and steam features of Java 8.
package net.clarenceho.euler;

import java.util.stream.IntStream;

public class Multiples3and5 {

    public static void main(String args[]) {
        System.out.println("The answer is " +
            Multiples3and5.dividableStream(1000).sum());
    }
 
    static boolean dividable(int i) {
        boolean result = false;
        if (i % 3 == 0 || i % 5 == 0) {
            result = true;
        }
        return result;
    }
 
    static IntStream dividableStream(int max) {
        return IntStream.range(1, max)
            .filter(i -> dividable(i));
    }
}