[Mono-list] StackOverflowException with decimal

Brandon Perry bperry.volatile at gmail.com
Sat Jun 21 18:08:48 EDT 2008


Hi, I am doing some of the problems at Project Euler using Mono and C#. I am
on the second problem, but I get a StackOverflowException error everytime.
The code is as follows:

        public decimal Solve()
        {
            decimal c, i = 0;
            decimal sum = 0;
            decimal cap = 4000000;

            for (;i <= cap;i++)
            {
                if (i <= 2)
                {
                    sum += 1;
                }
                else
                {
                    try
                    {
                        c = Fib(i);

                        if (!Convert.ToBoolean(c%2))
                        {
                            sum += c;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }

            return sum;
        }

        public decimal Fib(decimal n)
        {
            return Fib(n-1) + Fib(n-2);
        }

The stacktrace is here:
http://volatileminds.net/projects/euler/stacktrace.txt

I haven't had a chance to test this on .NET, but thought I would get a
second opinion as to whether it is a PEBKAC error or a bug.

Any thoughts?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080621/54bffaa9/attachment.html 


More information about the Mono-list mailing list