コード例 #1
0
        public void GetAmicableNumber_Throws_Exception_When_Input_Number_Is_Not_Part_Of_Amicable_Pair()
        {
            var    inputNumber       = 13;
            Action getAmicableNumber = () => GeneralHelpers.GetAmicableNumber(13);

            getAmicableNumber.Should().Throw <NotPartOfAmicablePairException>()
            .WithMessage($"{inputNumber} is not part of an amicable pair.");
        }
コード例 #2
0
ファイル: Solution021.cs プロジェクト: OpenNOX/Solutions
        /// <summary>
        /// Run solution for problem 21.
        /// </summary>
        /// <param name="rangeThreshold">Range threshold.</param>
        /// <returns>Sum of amicable numbers below <paramref name="rangeThreshold"/>.</returns>
        public static int Run(int rangeThreshold)
        {
            var amicableNumbersSum = 0;

            for (var i = 3; i < rangeThreshold; i += 1)
            {
                try
                {
                    var amicableNumber = GeneralHelpers.GetAmicableNumber(i);

                    if (amicableNumber != 0)
                    {
                        amicableNumbersSum += i + amicableNumber;

                        i = amicableNumber;
                    }
                }
                catch (NotPartOfAmicablePairException)
                {
                }
            }

            return(amicableNumbersSum);
        }
コード例 #3
0
 public void GetAmicableNumber_Returns_Input_Numbers_Amicable_Number()
 {
     GeneralHelpers.GetAmicableNumber(220).Should().Be(284);
 }