Euler Problem 197


Euler Problem 197: Investigating the behaviour of a recursively defined sequence

Rješenje: Vrlo nezgodna formula i naivno se može potrošiti puno vremena smišljajući rješenje. Potrebno je izlistati prvih nekoliko stotina članova i uvidjeti da je niz već konvergirao i da se rješenje vidi, samoga treba sabrati. Kod implementacije sam uzeo broj 10000 i 10001, čisto proizvoljno može se uzeti i manji broj, odnosno jedan paran a jedan neparan uzastopan broj.

C# implementacija:

class Program
    {
        static void Main(string[] args)
        {
            double u0 = -1;
            double un = 0;
            double un_1=u0;
            double sum=0;
            for (int i = 1; i <= 10000+1; i++)
            {
                un = f(un_1);
                if (i == 10000 || i == 10001)
                    sum = sum + un;
                un_1 = un;
            }
            Console.WriteLine("Solution PE197={0}",sum);
            Console.WriteLine("Press any key to continue ... ");
            Console.Read();
        }

        static double f(double x)
        {
            return Math.Floor(Math.Pow(2, 30.403243784 - x*x))*Math.Pow(10,-9);
        }
    }

About Bahrudin Hrnjica

PhD in Mechanical Engineering, Microsoft MVP for .NET. Likes .NET, Math, Mechanical Engineering, Evolutionary Algorithms, Blogging.

Posted on 05/03/2010, in C#, Project Euler and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s