🍻


Project-Euler / 2

Problem 2 asks:

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Another straight forward problem, here's my solution:

public class Problem2 {
    public static void main(String[] args) {
        int n = 1, m = 2;
        int sum = 0;
        int temp;
        while (m <= 4_000_000) {
            if (m % 2 == 0) sum += m;
            temp = n;
            n = m;
            m += temp;
        }
        System.out.println(sum);
    }
}

With n and m being the nth and (n+1)th term in the sequence respectively.

Last updated on .