Project Euler 74


Project Euler 74:

Determine the number of factorial chains that contain exactly sixty non-repeating terms.

Solution: Uz malu optimizaciju koda rješenje se dobija u vremenu oko minute.

static void Main(string[] args)
{
    long counter = 0;
    List<long> series = new List<long>();
    for (int i = 1; i < 1000000; i++)
    {
        long n=i;
        int tempCounter = 1;
        series.Clear();
        series.Add(n);
        while (true)
        {
            n = Nextterm(n);
            if (series.Contains(n))
                break;
            series.Add(n);
            tempCounter++;
           
        }
        if (tempCounter == 60)
            counter++;
    }
    Console.WriteLine(counter);
    Console.WriteLine("Press any key to continie...");
    Console.Read();
}
static long Nextterm(long n)
{
    var str = n.ToString();
    long numb=str.Sum(x=>Factorial(long.Parse(x.ToString())));
    return numb;
}
static long Factorial(long n)
{
    long fact=1;
    for (int i = 1; i <= n; i++)
        fact *= i;
    return fact;
}

About Bahrudin Hrnjica

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

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