Skip to content

Instantly share code, notes, and snippets.

@unserializable
Created June 1, 2017 07:20
Show Gist options
  • Save unserializable/acf0c5a075f96476071900f60b7458b5 to your computer and use it in GitHub Desktop.
Save unserializable/acf0c5a075f96476071900f60b7458b5 to your computer and use it in GitHub Desktop.
Sum even Fibonacci numbers not exceeding 4 million (Project Euler Problem #2)
/*
Minimalistic (length-wise) looping Java code for Project Euler problem #2 (https://projecteuler.net/problem=2).
Finds the sum of /even/ Fibonacci numbers which do not exceed 4 million.
^= operater use for variable swap in Java cannot be as compact as in C, see:
http://stackoverflow.com/questions/3844934/why-is-this-statement-not-working-in-java-x-y-x-y
@author Taimo Peelo
*/
public class SumEvenFibo_lt_4M {
public static void main(String[] args) { // Output is 4613732
for (long s = 0, f[] = {0, 1}; f[1] <= 4_000_000L || System.out.printf("%d%n", s).checkError(); f[0] ^= f[1]^(f[1]=f[0]), s += (f[1] += f[0]) % 2 == 0 ? f[1] : 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment