The largest prime factor with php

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-05-19T18:25:54Z Indexed on 2010/05/19 18:40 UTC
Read the original article Hit count: 157

Filed under:
|

So, I wrote php program to find the largest prime factor with php and I think it is quite optimal, because it loads quite fast. But there is a problem, it doesn't count very big numbers's prime factors. Here is a program:

function is_even($s) {

    $sk_sum = 0;

    for($i = 1; $i <= $s; $i++) {

        if($s % $i == 0) { $sk_sum++; }     

    }

    if($sk_sum == 2) {

        return true;

    }

}

$x = 600851475143; $i = 2; //x is number

while($i <= $x) {

        if($x % $i == 0) {

            if(is_even($i)) {

                $sk = $i; $x = $x / $i;

            }

        }

    $i++;

}

echo $sk;

© Stack Overflow or respective owner

Related posts about php

Related posts about logical