public int NumberOfDivisors(long number) { int numberOfDivisors = 1; List <int> primeFactors = _primeService.GetPrimeFactors(number).ToList(); // number of divisors d(n) = (a+1)(b+1)(c+1)... // where n = p^a * q^b * r^c ... (expressed as a product of prime factors) while (primeFactors.Count > 0) { int a = primeFactors[0]; primeFactors.RemoveAt(0); int countOfA = 1; // Remove all other occurences of a. while (true) { if (primeFactors.Contains(a)) { ++countOfA; primeFactors.Remove(a); continue; } break; } numberOfDivisors *= countOfA + 1; } return(numberOfDivisors); }
public override string GetAnswer() { var numbers = new Queue <List <int> >(); numbers.Enqueue(_primeService.GetPrimeFactors(1).ToList()); numbers.Enqueue(_primeService.GetPrimeFactors(2).ToList()); numbers.Enqueue(_primeService.GetPrimeFactors(3).ToList()); for (int i = 4; ; ++i) { List <int> newFactors = _primeService.GetPrimeFactors(i).ToList(); CondenseDuplicates(newFactors); numbers.Enqueue(newFactors); bool found = true; foreach (List <int> factors in numbers) { if (factors.Count != 4) { found = false; break; } } if (found) { break; } numbers.Dequeue(); } List <int> primeFactors = numbers.Dequeue(); int product = 1; foreach (int factor in primeFactors) { product *= factor; } return(product.ToString()); }