0

I'm solving problems from Eurler's page. I'm actually spending a lot of time trying to do something with my code. It's outputting fibonacci numbers correctly, and also summing it(for 15 numbers). But When I put limit to 4 000 000 numbers, then it shows me output like this: -1833689714. I changed int to uint or large but still answer is not correct. Here is link to problem: https://projecteuler.net/problem=2https://projecteuler.net/problem=2

And here my code:

class Program
{
    static void Main(string[] args)
    {
        int a = 0;
        int b = 1;
        int fibonacciNumber = 0;
        int sum=0;
        bool whichNumber = false;

        for(int i = 0; i < 4000000; i++)
        {               
            fibonacciNumber = a + b; 
            if (fibonacciNumber % 2 == 0) sum += fibonacciNumber; //adding to sum of even-numbers

            if (whichNumber == false) //operations to change numbers 
            {
                a = fibonacciNumber;
                whichNumber = true;
            }

            else if (whichNumber == true)
            {
                b = fibonacciNumber;
                whichNumber = false;                    
            }
        }
        Console.WriteLine(sum);
        Console.ReadLine();
    }
}

Does someone see where I made a mistake?

  • You probably want to trace through, and see what the numbers are as you go along. If you aren't using a debugger, adding good ol' Console.WriteLine() calls into your code will tell you where your code is breaking. It sounds like you are trying to learn to code, which is great. Debugging is among the most important skills you'll need to learn - probably nearly as important as writing code yourself, as you are likely to have to debug many other people's code over your career. – Wonko the Sane Dec 11 '18 at 20:32
  • If you use `System.Numerics.BigInteger` and you have a `for` loop with `i < 40000` then you get a number back that has `8360` digits. You cannot use `int`, `long`, `ulong`, etc, as your numbers you are trying to compute are simply too large. – Enigmativity Dec 11 '18 at 20:37
  • 1
    It seemed odd to me that a problem from project Euler would required a BigInteger so I've read the problem description. You don't need first 4 million items, but rather first N items that don't exceed 4 million. A lot smaller list. – Dialecticus Dec 11 '18 at 20:42
  • See these pages: [The 1000th Fibonacci Number](http://mathforum.org/library/drmath/view/52700.html) and [The first 300 Fibonacci numbers](http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html). Based on that you can define tests to test and debug your code. – Jeroen Heier Dec 11 '18 at 20:49

3 Answers3

4

Try with long instead of int. It will take you a few more steps, but there will be a limit again. If you want really big numbers then you need a type that can handle them like BigInteger.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
2

Read the problem again. You don't need to sum the first four million Fibonacci numbers. Only the numbers, that are smaller than four million.

You will have to change your implementation, but you don't need bigger data types like BigInteger, int is big enough.

Streamfighter
  • 454
  • 4
  • 15
-1

It looks like your sum variable needs to be a long. The value is larger than what an int can hold.

ps2goat
  • 8,067
  • 1
  • 35
  • 68