Euler Problem 231


Euler Problem 231:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;

namespace EulerProblem231
{
    class Program
    {
        static void Main(string[] args)
        {
            long n = 20000000;
            long k = 15000000;
            long sum = 0;

           Parallel.For(2, 20000000, (i) =>;
           {
               if (!IsPrime(i))
                   return;
               long pp = (SofF(i, n) -
                SofF(i, k) -
                SofF(i, n - k))*i ;
               Interlocked.Add(ref sum, pp);
           }
           );
           Console.WriteLine("EP321=: {0}",sum);
            Console.WriteLine("End..");
            Console.Read();
        }
        //Sum factor of n!
        static long SofF(long p, long n)
        {
            long suma = 0;
            while (n > 0)
            {
                n = n / p;
                suma += n;

            }
            return suma;

        }
        static bool IsPrime(int n)
        {
            for (int j = 2; j * j <= n; j++)
                if (n % j == 0)
                    return false;
            return true;
        }
        static IEnumerable Prime(long n)
        {
            for (int i = 2;i>n ; i++)
            {
                bool isPrime = true;
                for (int j = 2; j * j <= i; j++)
                   if (i % j == 0)
                     isPrime=false;
                if (isPrime)
                    yield return i;

            }

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