php - 找到特定数字的质因数

标签 php algorithm factors

我写的代码有什么问题? 我正在尝试解决 euler 项目中的一个问题。

谢谢你:)

我在代码中添加了引号,这样您就可以理解我所做的一切。它适用于号码 13915,但不适用于我需要的号码。

问题是我检查数字被分成 3,虽然它不是一个完整的数字,但它仍然说它是一个质数。

例如:

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

最佳答案

您不必检查每一步的素性。 也许更简单:

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

?>

关于php - 找到特定数字的质因数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025872/

相关文章:

php - 使用 $_POST 更新 $sql_query 从 AJAX 到 PHP 的值

algorithm - 整数组合数与特定列表中的数字

algorithm - 如何在 O(n) 中对具有给定范围的列表进行排序

c# - 找到一个数的最大质因数的最佳方法是什么?

c# - 将数字分解为 2 个质数余因子

php - Joomla - 在哪里编辑插入 SQL?

javascript - 如何将 php 对象获取到 javascript?

php - MySQL 更新 SET 替换为 PHP 多个查询

algorithm - 基于 Lua 中的键对数组值进行分组?

c++ - 如何找到时间复杂度小于 N 的给定数字 N 的所有因子?