This is a program to find prime factors of a numbers.
Like - Prime factors for number 1125 are 3
and 5
My Algo this goes way - (and please let me know if it is not correct)
- Firstly I am finding a square root of the number using
sqrt()
function to break the complexity and run time. - To find prime numbers in between the range.
- Lastly to divide these prime numbers with the original number(yet not reached to this step as failing in the second step.
My code which is not working, let me know where exactly I am failing in my logic and code from step 2 and step 3.
No error thrown but the code is also not outputting anything.
<?php
error_reporting(E_ALL);
$number = 6006;
$sqrt_num = (int)sqrt($number);
for($i=2;$i<$sqrt_num;$i++)
{
for($j=2;$j<=$i-1;$j++)
{
if($i%$j==0)
break;
if($i==$j)
echo $i;
}
}