Here is the C# Solution to find the factors of given number:

– To improve the Performance of finding Factors of a given Odd number, we can increment i in the for loop by 2 instead of 1.

 public List findFactors(int n)
        {
            List factors = new List();

            int max = Convert.ToInt32(Math.Sqrt(n));

            for (int i = 1; i <= max; i++)
            {
                if(n % i == 0)
                {
                    factors.Add(i);
                    if(i != max) // add square-root factors only once.
                        factors.Add(n/i);
                }
            }
            return factors;
        }

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *