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;
}
Advertisement

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 )

Facebook photo

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

Connecting to %s