Skip to content

Instantly share code, notes, and snippets.

@DavidCulpepper
Last active August 29, 2015 13:55
Show Gist options
  • Save DavidCulpepper/8733720 to your computer and use it in GitHub Desktop.
Save DavidCulpepper/8733720 to your computer and use it in GitHub Desktop.
Even Fib
import java.util.Arrays;
import java.util.List;
public class EulerModel {
public int sumSquareDifference(List<Integer> nums) {
int sum = 0;
int sumOfSquares = 0;
for (int i : nums) {
sum += i;
sumOfSquares += (i * i);
}
return (sum * sum) - sumOfSquares;
}
public int sumSquareDifference(Integer... nums) {
return sumSquareDifference(Arrays.asList(nums));
}
public int evenFib() {
boolean notTooBig = true;
int sum = 0;
int a = 1;
int b = 0;
int x = 1;
while (notTooBig) {
x = a + b;
b = a;
a = x;
if (x <= 4000000) {
if (x % 2 == 0) {
sum += x;
}
} else {
notTooBig = false;
}
}
return sum;
}
public int evenFib(int threshold) {
int first = 2;
int second = 1;
int sum = 0;
int temp;
while (first <= threshold) {
if (first % 2 == 0) {
sum += first;
}
temp = first + second;
second = first;
first = temp;
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment