10
$\begingroup$

I'm trying to solve the third Project Euler problem and I'd like a little help understanding a mathematical concept underlying my tentative solution.

The question reads:

The prime factors of 13195 are 5, 7, 13, and 29.

What is the largest prime factor of the number 600851475143 ?

As a caveat, in accordance with the wishes of Project Euler I won't be providing any code, my question is centered on why a mathematical concept works.

Failed Attempt

My first algorithm looked at all the numbers from 1 through 600851475143, but was unable to complete the subsequent computations (concerning primes and factorization) due to memory constraints.

Successful Attempt

My next algorithm looked at all the numbers from 1 through $\sqrt{600851475143}$ and completed the computation successfully.

My Question

Why does evaluating $\sqrt{600851475143}$ work in this instance when I really want to evaluate up to 600851475143 ? How can I be sure this approach won't miss some factor like $2 \cdot n$ or $3 \cdot n$, when $n$ is some number between $\sqrt{600851475143}$ and 600851475143 ?

$\endgroup$
  • 7
    $\begingroup$ If a number $N$ has a prime factor larger than $\sqrt{N}$ , then it surely has a prime factor smaller than $\sqrt{N}$. $\endgroup$ – barak manos Nov 26 '14 at 11:46
  • 2
    $\begingroup$ @barakmanos You may as well write this as the answer. $\endgroup$ – DanielV Nov 26 '14 at 11:47
  • 1
    $\begingroup$ You won't miss $2\cdot n$ or $3\cdot n$ because you have checked for $2$ and $3$. $\endgroup$ – gammatester Nov 26 '14 at 11:48
  • 5
    $\begingroup$ We have to be a little careful in writing the program, since some plausible approaches will miss "large" prime factors when a small prime factor occurs with multiplicity $\gt 1$. $\endgroup$ – André Nicolas Nov 26 '14 at 11:59
  • 1
    $\begingroup$ You don't have to check all the way to $\sqrt{600851475143}$. Once you identify 71 as a factor you know that the largest prime factor of 600851475143, is also the largest prime factor of 600851475143/71=8462696833. So at that point you can limit the search to $\sqrt{8462696833}$ - and so on. Once you hit the square root you know, by the argument given in the answers, that the number you are taking the square root of is prime, and also that it's the largest prime factor of your original number. $\endgroup$ – Taemyr Nov 26 '14 at 15:22
17
$\begingroup$

If a number $N$ has a prime factor larger than $\sqrt{N}$ , then it surely has a prime factor smaller than $\sqrt{N}$.

So it's sufficient to search for prime factors in the range $[1,\sqrt{N}]$, and then use them in order to compute the prime factors in the range $[\sqrt{N},N]$.

If no prime factors exist in the range $[1,\sqrt{N}]$, then $N$ itself is prime and there is no need to continue searching beyond that range.

$\endgroup$
  • 2
    $\begingroup$ As mentioned by André Nicolas, prime factors with >1 multiplicity will result in the larger factor being non-prime. To solve this, keep dividing the larger factor of a pair by the smaller one until it no longer divides. $\endgroup$ – Mark K Cowan Nov 26 '14 at 14:00
  • $\begingroup$ As an example: For 28, factors are 2,2,7, here 7 is larger than sqroot(28), but there is no single prime number that can combine to form 28 (i.e. x*7=28, where x is prime, this does not exist, since x is 4 which is not prime), so dividing the primes from beginning with 2 multiple time will help, 28/2= 14, 14/2 = 7, then we know the 2 & 2 are prime factors, also take the remaining one 7 is also another prime number completing the prime factor list. best of luck. $\endgroup$ – Manohar Reddy Poreddy Dec 2 '15 at 0:25
12
$\begingroup$

If you do not find a factor less than $\sqrt{x}$, then $x$ is prime for the following reason. Consider the opposite, you find two factors larger than $\sqrt{x}$, say $a$ and $b$. But then $a\cdot b> \sqrt{x}\sqrt{x} = x$. Therefore, if there is a factor larger than $\sqrt{x}$, there must also exist a factor smaller than $\sqrt{x}$, otherwise their product would exceed the value of $x$.

Regarding the last question. You will not miss some factor like $2\cdot n$ because you have already checked if $2$ is a factor.

$\endgroup$
2
$\begingroup$

What you're describing is a prime-testing algorithm known as the sieve of Eratosthenes.

It seems like you already understand the concept:

  • Cross out $1$

  • Circle $2$ as the first prime, then cross out all of the multiples of $2$ in your list.

  • The next not-crossed-out number is the next prime (in this case $3$).

If your list has $N$ numbers, you only need to test until you get to a prime that's bigger than $\sqrt{N}$.


Other users have mentioned the reason for $\sqrt{N}$ being the maximum you need to test until, but I'll add an alternative explanation. If you consider listing out all of the factors of $N$, in order from least to greatest, you will either get an even number or an odd number. $N$ has an odd number of factors if and only if it is a perfect square.

In any case, $N$ always has the same number of factors (strictly) less than its square root as it does (strictly) greater than its square root.

In fact, there's an explicit bijection between the set of factors less than $\sqrt{N}$ and the set of factors greater than $\sqrt{N}$ given by $$f(a) = \frac{N}{a}$$

(where $a$ is a factor of $N$ less than $\sqrt{N}$)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy