Counting the Factors of a Number
By definition, a factor is one of two or more positive numbers that when multiplied together produce a given product. For example, given the integer 24, the count of factors 8.
The below algorithm tests the range of numbers from 1 to a given integer. If the given interger divided by the current number returns a remainder of 0, then the current number is a factor of the given integer.
public int GetFactorsCount(int i) { return Enumerable.Range(1, i).Count(x => i % x == 0); }
Get the source control, including unit tests: GetFactorsCount.zip (9.43 kb)
Getting All Factors for a Number
By definition, a factor is one of two or more positive numbers that when multiplied together produce a given product. For example, given the integer 24, the factors are [1, 2, 3, 4, 6, 8, 12, 24].
The below algorithm tests the range of numbers from 1 to a given integer. If the given integer divided by the current number returns a remainder of 0, then the current number is a factor of the given integer.
public List<int> GetFactors(int i) { return Enumerable.Range(1, i).Where(x => i % x == 0).ToList(); }
Get the source control, including unit tests: GetFactors.zip (9.85 kb)