Prime Factor
This algorithm finds all prime factors of given number.
public static List<int> PrimeFactor(int number)
{
List<int> factors = new List<int>();
while (number % 2 == 0)
{
factors.Add(2);
number /= 2;
}
for (int i = 3; i <= Math.Sqrt(number); i += 2)
{
while (number % i == 0)
{
factors.Add(i);
number /= i;
}
}
if (number > 2)
{
factors.Add(number);
}
return factors;
}
Example
List<int> factors = PrimeFactor(488);
Output
2
2
2
61