Project Euler 23 - Non-abundant sums

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:58   1397   0

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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP