Last active
August 29, 2015 13:55
-
-
Save DavidCulpepper/8733720 to your computer and use it in GitHub Desktop.
Even Fib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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