найти главный фактор для определенного числа

что не так с кодом, который я написал?
Я пытаюсь решить вопрос в проекте Эйлера.

благодарю вас 🙂

я добавил цитаты в код, чтобы вы могли понять все, что я сделал. он работает на номер 13915, но не на номер, который мне нужен.

проблема в том, что я проверяю число делится на 3, и хотя это не полное число, оно все равно говорит, что это простое число.

например:

3 является основным фактором 600851475143

новый номер: 200283825047.67

новый крупнейший премьер: 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}
";

1

Решение

Вам не нужно проверять каждый шаг на предмет первичности.
Может быть, проще:

<?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}
";

?>
4

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector