AppleKIdd
AppleKIdd

Reputation: 17

Find the sum of all even Fibonacci numbers, with user input

Currently I am in the middle of writing a program to find even Fibonacci numbers in a Windows Form Application(WPA), with user input.

When I execute my program, I come with different data in contrast to the test data that I have.

For example, When I type 100,000 as input, the output I am getting is 5500034 but it should be 60696.

The code of my program is as follows:

        int val1 = 1;
        int val2 = 2;
        Int64 evenTerms = 2;
        val2 = int.Parse(textBox3.Text);
        while (val2 < 5000000)
        {
            int temp = val1;
            val1 = val2;
            val2 = temp + val2;
            if (val2 % 2 == 0)
            {
                evenTerms += val2;
            }
        }
        MessageBox.Show("" + val2);

Can anyone help me sort out the problem?

Thanks.

Upvotes: 1

Views: 467

Answers (4)

Sherin Binu
Sherin Binu

Reputation: 520

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main() {

typedef unsigned long ulong;

ulong fib(ulong a, ulong b, ulong * odd_z, ulong n) {
    ulong c = a + b;
    if((c+b) >= n) { return 0; }
    if(a%2 == 0) { *odd_z+=(b+c); }
    return fib(b,c,odd_z, n);
}

int T;
scanf("%d",&T);
ulong odd_z = 0;
ulong *sum = &odd_z;

while(T--) {
    ulong N;
    scanf("%lu",&N);
    fib(0,1,&odd_z, N); 
    printf("%lu\n",*sum);
    *sum=0;
}
return 0;

}

This algorithm is also more time and space efficient

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

I suggest using generator to enumerate all the Fibonacci numbers:

public static IEnumerable<long> FiboGen() {
  long left = 0;
  long right = 1;

  yield return left;
  yield return right;

  while (true) {
    long result = left + right;

    yield return result;

    left = right;
    right = result;   
  }
}

and then Linq to sum up the required values only:

int limit = int.Parse(textBox3.Text);

//   60696 for the 1000000 limit
// 4613732 for the 5000000 limit
var result = FiboGen()            // take Fibonacci numbers 
  .Where(val => val % 2 == 0)     // but only even ones
  .TakeWhile(val => val < limit)  // and less than limit
  .Sum();                         // finally sum them up.

MessageBox.Show(result.ToString());

Upvotes: 1

Panther Coder
Panther Coder

Reputation: 1078

As far as I have understood your problem (The question is unclear), Hope this solution works :)

        int val1 = 0;
        int val2 = 1;
        Int64 evenTerms = 0;
        int val3 = int.Parse(textBox3.Text), val4 = 0, temp;
        if (val3 < 5000000)
        {
            while (val4 < val3){
              temp = val1 + val2;
              val1 = val2;
              val2 = temp;
              if (temp % 2 == 0)
              {
                  evenTerms += 1;
              }
              val4++;
            }
        }
        MessageBox.Show("" + evenTerms);

Upvotes: 0

Ashkan S
Ashkan S

Reputation: 11491

well, first of Fibonacci starts with 1,1,2,3,.... meaning that you are one step ahead of the list. You should start with val1=1, and val2=1;

https://en.wikipedia.org/wiki/Fibonacci_number

Then why do you use your input param as a part of your calculation?!!

Upvotes: 0

Related Questions