Getting Factors of a Number

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2010-12-28T21:01:39Z Indexed on 2010/12/28 21:54 UTC
Read the original article Hit count: 185

Filed under:
|
|

Hi

Problem: I'm trying to refactor this algorithm to make it faster. What would be the first refactoring here for speed?

public int GetHowManyFactors(int numberToCheck)
    {
        // we know 1 is a factor and the numberToCheck
        int factorCount = 2; 
        // start from 2 as we know 1 is a factor, and less than as numberToCheck is a factor
        for (int i = 2; i < numberToCheck; i++) 
        {
            if (numberToCheck % i == 0)
                factorCount++;
        }
        return factorCount;
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about math