/// <summary> /// Run solution for problem 12. /// </summary> /// <param name="divisorCountThreshold">Divisor count threshold.</param> /// <returns> /// First triangle number to have equal or greater than /// <paramref name="divisorCountThreshold"/>. /// </returns> public static int Run(int divisorCountThreshold) { var answer = 0; var divisors = Array.Empty <int>(); for (var i = 1; divisors.Length + 1 < divisorCountThreshold; i += 1) { answer += i; divisors = GeneralMath.GetProperDivisors(answer).ToArray(); } return(answer); }
/// <summary> /// Run solution for problem 23. /// </summary> /// <remarks> /// Abundant Number: /// Number where the sum of its proper divisors is greater than the number itself. /// /// All numbers greater than 28,123 can be written as the sum of two abundant numbers. /// </remarks> /// <returns> /// Sum of all numbers that cannot be written as the sum of two abundant numbers. /// </returns> public static int Run() { var nonAbundantNumbersThreshold = 28123; var abundantNumbers = new List <int>(); var abundantSums = new List <int>(); var nonAbundantSum = 0; for (var i = 1; i <= nonAbundantNumbersThreshold; i += 1) { if (GeneralMath.GetProperDivisors(i).Aggregate(0, (sum, divisor) => sum + divisor) > i) { abundantNumbers.Add(i); } nonAbundantSum += i; } var abundantNumbersSums = new HashSet <int>(); for (var i = 0; i < abundantNumbers.Count; i += 1) { for (var j = i; j < abundantNumbers.Count; j += 1) { var abundantNumbersSum = abundantNumbers[i] + abundantNumbers[j]; if (abundantNumbersSum <= nonAbundantNumbersThreshold) { abundantNumbersSums.Add(abundantNumbersSum); } else { break; } } } nonAbundantSum -= abundantNumbersSums.Aggregate(0, (sum, abundantNumbersSum) => sum + abundantNumbersSum); return(nonAbundantSum); }
public void GetProperDivisors_Returns_Input_Numbers_Proper_Divisors() { GeneralMath.GetProperDivisors(220).Should().BeEquivalentTo( new[] { 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110 }); }