Euler Problem 235


Euler Problem 235: An Arithmetic Geometric sequence.

Rješenje: Rješenje se vrlo lako dobiva sa Binary Search algoritmom. Nakon prikaza vrijednosti za proizvoljne r-ove,zaključujemo da ograničenja možemo staviti proizvoljno između –2 i 2, jer konvergencija broja r se kreće oko 1.  Prikazana je implementacija u C#.

namespace EulerProblem235
{
    class Program
    {
        static void Main(string[] args)
        {
            double low = -2.0, heigh = 2.0;
            double r=0;
            double val = -600000000000.0;
            double prevr = double.NaN;
            for(int i = 0; ; i++)
            {
                r = (low + heigh) / 2;
                double ss=Summa(r);
                if (val < ss)
                    low = r;
                else
                    heigh = r;
                if (r == prevr)
                    break;
                prevr = r;

            }

            Console.WriteLine(Math.Round(r,12).ToString());
            Console.Read();
        }

       static double Summa(double r)
        {
            double sum = 0.0;
            for(double k = 1; k <= 5000; k++)
                sum += (900.0 - 3.0 * k) * Math.Pow(r, k - 1.0);
            return sum;
        }
    }
}

About Bahrudin Hrnjica

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

Posted on 06/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