A
A
adun32014-10-29 19:26:52
PHP
adun3, 2014-10-29 19:26:52

Finding the largest prime divisor?

Good afternoon! I want to find the largest prime divisor of the number 600851475143.
how do we do it? we are looking for all prime numbers up to the root (600851475143), i.e. up to 775147, and then we check the divisibility of 600851475143 by these prime numbers. but for some reason it gives out 3 and 29 (and this and that is not a divisor). please tell me where I'm stupid, I just started to screw up, if that.

define("LIMIT",775147);
    define("SQRT_LIMIT",floor(sqrt(LIMIT)));
    
    $S = str_repeat("\1", LIMIT+1);

    for($i=2;$i<=SQRT_LIMIT;$i++){
    if($S[$i]==="\1"){
        for($j=$i*$i; $j<=LIMIT; $j+=$i){
            $S[$j]="\0";
            }
        }
    }
    for($i=2;$i<=LIMIT;$i++)
    {
        if($S[$i]=="\1" && (600851475143 % $i) == 0)
        {
            echo $i.' ';
        }
    }
        ?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sumor, 2014-10-29
@Sumor

The number 600851475143 is over 4 bytes.
If you take only 4 bytes from this number, then you get the number -443946297, which is exactly divisible by 3 and 29.
That is, your program works only with four-byte numbers.

B
brutal_lobster, 2014-10-29
@brutal_lobster

Hm. str_repeat is completely confusing - it's a very interesting solution) but completely inadequate.
To store a list of prime numbers - use an array (array) of these very numbers :)
Better arm yourself with a book on the language and take the dumbest algorithm for factoring into prime factors - the one in which we divide (if divisible, of course) the number by all numbers starting from 2, until it will not become equal to 1.
And you can understand where you have it wrong - choose a smaller number and implement your algorithm yourself on a piece of paper.

A
adun3, 2014-10-30
@adun3

for($i=2;$i<=LIMIT;$i++)
{
$ostatok = bcmod('600851475143', $i);

if($S[$i]=="\1" && $ostatok == 0)
{
echo $i.' ';
}

in general, I want to put 2 fingers from my algorithm in my mouth, but nothing, the main thing was everything :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question