GabiArzu
GabiArzu

Reputation: 29

find the prime factor for a specific number

what is wrong with the code i wrote? im trying to solve a question in the euler project.

thank you :)

i added quotes to the code so you can understand everything i have done. it works for the number 13915 but not for the number i need.

the problem is that im checking the the number is divided in 3, and although its not a complete number it still says its a prime.

for example:

3 is a prime factor of 600851475143

new number: 200283825047.67

new largest prime: 3

 <?php
/**
 * Created by PhpStorm.
 * User: gabia_000
 * Date: 13/08/2015
 * Time: 23:32
 *
 * Project euler - Question 3 ;
 * The prime factors of 13195 are 5, 7, 13 and 29.
 * What is the largest prime factor of the number 600851475143 ? ;
 */

    $num = 600851475143; // The number that contains the factors ;
    $largest = 1; // the largest prime factor ;
    $n = 3; // the count of number starting 3 to find the prime factor ;
    echo '<b>number - '.$num.'</b><br /><br />'; // DISPLAY ;
    while ($num > 1) { // as long as the number isnt 1 yet ;
        $prime = 1; // defult true - the $n is prime;
        $i = 2; // number that the prime is divide by ;
        while($i < $n && $prime == 1) { // as long as $n is bigger then $i and prime is still true ;
            if($n % $i == 0) { // if $n is divided in $i ;
                $prime = 0; // prime false ;
                echo "{$n} not a prime, divided by {$i}.<br />"; // DISPLAY ;
                break 1; // break the while ;
            }
            $i = $i + 1; // increase the $i by 1 ;
        }
        if ($prime) { // if the prime is still true ;
            if($num % $n == 0) { // if the prime number is a factor of the number ;

                echo "{$n} is a prime factor of {$num}<br />"; // DISPLAY ;
                $num = $num / $n; // set the new number ;
                echo " - new number: {$num}<br />"; // DISPLAY ;
                $largest = $n; // set the new largest prime factor ;
                echo " - new largest prime: {$largest}<br />"; // DISPLAY ;

            }
            else { // if the prime number isnt a factor of the number ;
                echo "{$n} is not a prime factor of {$num}<br />"; // DISPLAY ; 
            }
        }
        $n = $n + 1; // increase the $n by 1 ;
        if($n > $num) { // if the $n is bigger then the number is there is no prime factor ;
            break ; // break the while ;
            echo "<br /><br /><b>no prime factors for </b>{$num}}"; // DISPLAY ;
        }
    }
    // DISPLAY END RESULTS ;
    echo "
        latest \$n - <b>{$n}</b><br />
        largest prime - <b>{$largest}</b><br />
        number - {$num}
    ";

Upvotes: 1

Views: 704

Answers (1)

Rasul Kerimov
Rasul Kerimov

Reputation: 549

You don't have to check every step for for primality. Maybe more simple:

<?php
/**
 * Created by PhpStorm.
 * User: gabia_000
 * Date: 13/08/2015
 * Time: 23:32
 *
 * Project euler - Question 3 ;
 * The prime factors of 13195 are 5, 7, 13 and 29.
 * What is the largest prime factor of the number 600851475143 ? ;
 */

    $num = 600851475143; // The number that contains the factors ;
    $num_save = $num;
    $largest = 1; // the largest prime factor ;
    $n = 2; // the count of number starting 3 to find the prime factor ;
    echo '<b>number - '.$num.'</b><br /><br />'; // DISPLAY ;


    while ($num > 1) { // as long as the number isnt 1 yet ;

        if($num % $n == 0) {
            $largest = $n;

            while($num % $n == 0) {
                $num = $num / $n;
            }
        }
        $n = $n + 1;
    }
    echo "
        latest \$n - <b>{$n}</b><br />
        largest prime - <b>{$largest}</b><br />
        number - {$num_save}
    ";

?>

Upvotes: 4

Related Questions