public static long Solution() { long solution = 0; for (long a = (long)Math.Ceiling(limit / 3.0); a < limit; a++) { for (long b = (long)Math.Ceiling((limit - a) / 2.0); b <= a; b++) { var c = limit - a - b; var powerOfFive = UtilityFunctions.LargestPowerDividingFactorial(limit, 5) - UtilityFunctions.LargestPowerDividingFactorial(a, 5) - UtilityFunctions.LargestPowerDividingFactorial(b, 5) - UtilityFunctions.LargestPowerDividingFactorial(c, 5); var powerOfTwo = UtilityFunctions.LargestPowerDividingFactorial(limit, 2) - UtilityFunctions.LargestPowerDividingFactorial(a, 2) - UtilityFunctions.LargestPowerDividingFactorial(b, 2) - UtilityFunctions.LargestPowerDividingFactorial(c, 2); if (powerOfFive >= 12 && powerOfTwo >= 12) { solution += 6; if (a == b || b == c) { solution -= 3; if (a == b && b == c) { solution -= 2; } } } } } return(solution); }
public static long Solution() { var stopwatch = new Stopwatch(); long solution = 0; var primes = UtilityFunctions.Primes(limit); var minima = new long[limit + 1]; minima[0] = 1; foreach (var p in primes) { long primePower = p; long exponent = 1; while (primePower <= limit) { long upper = exponent; long lower = 1; while (upper - lower > 1) { long mid = (upper + lower) / 2; if (UtilityFunctions.LargestPowerDividingFactorial(p * mid, p) < exponent) { lower = mid; } else { upper = mid; } } minima[primePower] = (upper) * p; stopwatch.Start(); for (long i = 1; i <= limit / primePower; i++) { minima[i * primePower] = Math.Max(minima[i], minima[primePower]); } stopwatch.Stop(); primePower *= p; exponent += 1; } } Console.WriteLine($"Populating table took {stopwatch.ElapsedMilliseconds}."); for (long i = 1; i < limit; i++) { solution += minima[i + 1]; } return(solution); }