Prime Factor

This algorithm finds all prime factors of given number.



									function PrimeFactor($number)
{
	$factors = array();

	while ($number % 2 == 0)
	{
		array_push($factors, 2);
		$number /= 2;
	}

	for ($i = 3; $i <= sqrt($number); $i += 2)
	{
		while ($number % $i == 0)
		{
			array_push($factors, $i);
			$number = (int)($number / $i);
		}
	}

	if ($number > 2)
	{
		array_push($factors, $number);
	}

	return $factors;
}
								


Example

									$factors = PrimeFactor(488);
								


Output

									2
2
2
61