Project Euler 23 - Non-abundant sums

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

-----solution-----

public class Problem23 : IProjectEulerSolver
    {
        const long MaxAbNum = 28123;
        public void Solve()
        {
            HashSet<long> allAboundantNumSet = new HashSet<long>();            
            for(int index=12;index<MaxAbNum;index++)
            {
               var isAbNum = CommonCalc.IsAbundantNumber(index);
                if(isAbNum)
                {
                    allAboundantNumSet.Add(index);
                }
            }

            var addedNumSet = new HashSet<long>();
            foreach (var first in allAboundantNumSet)
            {
                foreach (var second in allAboundantNumSet)
                {                    
                    var addedNum = first + second;
                    if (addedNum > MaxAbNum) continue;
                    //remove
                    if (!addedNumSet.Contains(addedNum))
                    {
                        addedNumSet.Add(addedNum);
                    }
                }
            }

            long sumVal = 0;
            for (int i = 1; i < MaxAbNum; i++)
            {
               if(!addedNumSet.Contains(i))
                {
                    sumVal += i;
                }
            }

            Console.WriteLine("count of allAboundantNumSet is {0}", allAboundantNumSet.Count);
            Console.WriteLine("count of addedNumSet is {0}", addedNumSet.Count);
            Console.WriteLine(sumVal);
        }
    }
public class CommonCalc
    {
        /// <summary>
        /// 獲取所有的因數
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static HashSet<long> GetAllProperDivisors(long number)
        {
            HashSet<long> factorList = new HashSet<long>();
            for(long index=1;index<number;index++)
            {
                if(number%index ==0 && !factorList.Contains(index))
                {
                    factorList.Add(index);
                }
            }
            return factorList;
        }

        
        public static bool IsAbundantNumber(long number)
        {
            var fatorSet = GetAllProperDivisors(number);

            return fatorSet.Sum() > number;
        }
    }

--result--

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章