コード例 #1
0
        /// <summary>Gets a list of all factors of specified numbers.</summary>
        /// <param name="inputNumber"></param>
        private IEnumerable <long> GetAllPrimeFactors(long inputNumber)
        {
            var  primeDivisors           = new List <long>(); //The return value
            var  currentlyAnalysedNumber = inputNumber;
            bool exitflag = true;

            while (exitflag)
            {
                var allFactors = primeSolver.GetDivisors(currentlyAnalysedNumber);

                var onlyPrimeFactors = from x in allFactors
                                       where primeSolver.IsPrime(x)
                                       select x;

                primeDivisors.AddRange(onlyPrimeFactors);

                var mul = primeDivisors.Aggregate((x, mult) => x * mult); //Multiply all found primes

                long temp = inputNumber / mul;
                if (temp > 1)
                {
                    currentlyAnalysedNumber = temp;
                }
                else //If remainder is 1 (or less - unlikely) break the loop;
                {
                    exitflag = false;
                }
                // || (mul == inputNumber)
            }
            return(primeDivisors);
        }
コード例 #2
0
            /// <summary>
            /// Returns true if the candidate is a composite number. Otherwise it returns false.
            /// </summary>
            /// <param name="candidate"></param>
            /// <returns>True if the candiate is a composite number. False otherwise.</returns>
            private bool IsComposite(long candidate)
            {
                var divisors = primeSolver.GetDivisors(candidate);

                if (divisors.Count < 3)
                {
                    return(false);                    //If the candidate has only two divisors (1 and itself), then it is ont composite.
                }
                return(true);
            }