Reputation: 93
public class Euler2 {
public static void main(String[] args) {
int Num1 = 0;
int Num2 = 1;
int sum = 0;
do
{
sum = Num1 + Num2;
Num1 = Num2;
Num2 = sum;
if (Num2 % 2 == 0)
sum = sum + Num2;
}
while (Num2 < 4000000);
System.out.println(sum);
}
}
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
I don't feel like I coded it wrong but the answer I'm getting is 5702887 and I know it should be 4613732.
Upvotes: 3
Views: 22620
Reputation: 7626
public class FibonacciUpto4Million {
public static void main(String[] args) {
int a=1, b=2;
int sum = 0;
while(b <= 4000000) {
//swap two variables
a = a+b;
b = a-b;
a = a-b;
if(a%2 == 0) {
sum = sum + a;
}
//make b = a+b;
b = a+b;
}
if(b % 2 == 0 && b <= 30) {
sum = sum + b;
}
System.out.println(sum); //4613732
}
}
Upvotes: 0
Reputation: 11522
public class Euler {
public static void main(String[] args) {
int num1 = 0;
int num2 = 1;
int temp = 0;
int sum = 0;
do {
if (num2 % 2 == 0) {
sum = sum + num2;
}
temp = num1 + num2;
num1 = num2;
num2 = temp;
} while (num2 < 4000000);
System.out.println(sum);
}
}
You messed up the sum by assigning it twice on each iteration where num2 is even. In this solution we use a temporary variable to store the next fibonacci number.
Solution = 4613732
Upvotes: 6
Reputation: 53
This might help you...
#include<stdio.h>
int main()
{
int i,a = 0,b = 1,temp = 0,sum = 0;
while(temp < 4000000)
{
temp = a + b;
printf("%d\n",temp);
a = b;
b = temp;
if(temp % 2 == 0)
{
sum = sum + temp;
}
}
printf("The sum is :- %d\n", sum);
return 0;
}
Upvotes: 0
Reputation: 1
This should work:
public static void main(String[] args)
{
int n1=0;
int n2=1;
int n3=0;
int count=10000;
int limit=4000000;
int sum=0;
for(int i=1;(i<=count && n3<=limit); i++)
{
n3=n1+n2;
if(n3%2==0){
sum = sum+n3;
System.out.println(sum);
}
n1=n2;
n2=n3;
}
}
Upvotes: -1
Reputation: 369
public void execute() {
int total = 1;
int toBeAdded = 1;
int limit = 4000000;
int totalSum = 0;
int temp = 0;
while (total <= limit) {
if (total % 2 == 0) {
totalSum = totalSum + total;
}
temp = toBeAdded;
toBeAdded = total;
total = toBeAdded + temp;
}
}
Upvotes: 1
Reputation: 56666
Another solution, using:
while
instead of do-while
bits
operation instead of %
only 2 variables to keep the effective values (no aux
):
public static void main(String[] args) {
int sum = 0 ;
int x1 = 1;
int x2 = 2;
while ( x1 < 4000000 ) {
if ( (x1 & 1) == 0 ){ // x % 2 == 0
sum += x1;
}
x2=x1+x2; // x2 = sum
x1=x2-x1; // x1 = the old value of x2
}
System.out.println(sum);
}
Upvotes: 1
Reputation: 8069
JavaScript:
function printSumOfEvenFiboNumbersWithin (limit) {
var current = 2, prev = 1, next, sum = 0;
do {
next = current + prev;
prev = current;
current = next;
sum += prev % 2 == 0 ? prev : 0;
} while (prev < limit);
console.log(sum);
}
printSumOfEvenFiboNumbersWithin(4000000); // 4613732
Upvotes: -2
Reputation: 671
I just made it and I can only tell you that you need an if(... % 2 == 0){} and only sum up the even numbers.... I hope it helps you.
Upvotes: 0
Reputation: 3896
Here are some hints (and not a complete answer since this appears to be homework):
if (Num2 % 2 == 0)
means that, as user2573153 pointed out, the value of sum
is getting doubled everytime Num2
is even. But this isn't how Fibonacci works. I can see that you're trying to continuously sum up the even terms by using that if
statement. So how can you do that without messing up sum
? (Hint: store up the even numbers somewhere else).Upvotes: 0
Reputation: 254
I'm not sure what the program is supposed to do but part of the problem may be that you are assigning num2 to the value of sum before your if statement, making the inside of that if statement equivalent to sum = sum + sum;
On another note it is bad practice to capitalize the names of your local variables. Good luck!
Upvotes: 0