コード例 #1
0
        static void Main(string[] args)
        {
            List <Task> tasks = new List <Task>();

            for (long i = 1; i < 999999; i++)
            {
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    long workNumber      = i;
                    List <long> sequence = new List <long>();
                    NumberSenseTools.CollatzSequence(workNumber, sequence);
                    int sequenceCount = sequence.Count();
                    lock (lockObj)
                    {
                        if (sequence.Count() > longestChain)
                        {
                            longestChain       = sequenceCount;
                            longestChainNumber = workNumber;
                        }
                    }
                }));
            }

            while (true)
            {
                bool finished = true;
                foreach (Task task in tasks)
                {
                    if (task.IsCompleted)
                    {
                        continue;
                    }
                    else
                    {
                        finished = false;
                        break;
                    }
                }

                if (finished)
                {
                    break;
                }
            }

            Console.WriteLine(String.Format("The longest sequence is {0}, the starting number was {1}", longestChain, longestChainNumber));
            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            FactorEnumerator factors      = new FactorEnumerator(testValue);
            List <long>      foundFactors = factors.ToList();

            long highestCurrentPrime = 0;

            foreach (long factor in foundFactors)
            {
                if (NumberSenseTools.IsAPrime(factor))
                {
                    if (factor > highestCurrentPrime)
                    {
                        highestCurrentPrime = factor;
                    }
                }
            }

            Console.WriteLine(String.Format("Anwser is {0}.", highestCurrentPrime));
            Console.ReadKey();
        }