Why does setting the condition to x*x < n not work in finding prime factors?

  • Thread starter Thread starter CrazyNeutrino
  • Start date Start date
  • Tags Tags
    Euler Project
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a trial division algorithm to find the largest prime factor of a number, specifically 600851475143. Participants explore the conditions under which the algorithm operates correctly, particularly the implications of using x*x < n versus x < n.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their implementation of trial division and notes that their code produces an incorrect result when using the condition x*x < n instead of x < n.
  • Another participant challenges the correctness of the initial results, stating that neither 6572 nor 1472 are factors of 600851475143 and suggests testing with smaller numbers.
  • A participant acknowledges a mistake in their output, clarifying that the correct answer is 6857 and expresses confusion about the failure of the x*x < n condition.
  • Another participant points out that updating the value of n within the loop affects the exit condition of the loop, which may explain the discrepancy when using x*x < n.
  • One participant provides a breakdown of the values of n during the factorization process, reinforcing that the condition x < n is appropriate in this context.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the results produced by the code and the implications of the conditions used in the loop. There is no consensus on the best approach, and the discussion remains unresolved regarding the optimal condition for the loop.

Contextual Notes

Participants note that the behavior of the algorithm changes based on the value of n being updated during the loop, which may affect the validity of the condition used for exiting the loop.

CrazyNeutrino
Messages
99
Reaction score
0
I've just started with project Euler. The problem I just finished is phrased as follows:

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

What is the largest prime factor of the number 600851475143 ?"

The method i used was trial division. Here is my code in C:
C:
#include<stdio.h>
int main()
{
     long long n = 600851475143;
     long long x;
     long long factor;

     for(x=2; x<n; x++)
     {
          while( n % x == 0 )
          {
                n =  n / x; 
                factor = x;
           }
     }

     printf("\n%lld\n", x);
     return 0;
}

Although this did solve the problem, (answer is 6572) I have read that you only need to check for factors upto the square root of n. I tried doing this by setting the condition x*x<n instead of x<n. This however, does not work and prints a value of 1472. Can someone explain why?
 
Last edited by a moderator:
Technology news on Phys.org
Your code is wrong. Neither 6572 nor 1472 are factors of 600851475143. Notice that the variable factor is never used. I suggest getting it working on smaller numbers first. Also, think about what happens if you find a factor x, and this leads to a new value of n=n/x which is smaller than x.
 
Last edited:
My apologies, i meant 6857. My program generated the correct answer. Admittedly I'm not too sure why and i don't know why it doesn't work when i try x*x<n. Also, i meant to print factor, not x.
 
CrazyNeutrino said:
My apologies, i meant 6857. My program generated the correct answer. Admittedly I'm not too sure why and i don't know why it doesn't work when i try x*x<n. Also, i meant to print factor, not x.

Does your original code (with x<n) work when you print factor?

Let me ask another question. Do you realize that when you update the value of n inside the for loop, that this impacts the test (x<n or x*x<n) which is done to decide when to exit the loop?
 
Last edited:
CrazyNeutrino said:
Although this did solve the problem, (answer is 6572) I have read that you only need to check for factors up to the square root of n. I tried doing this by setting the condition x*x<n instead of x<n. This however, does not work and prints a value of 1472. Can someone explain why?
600851475143 = 71×839×1471×6857.

During the loop the values for n are:

600851475143/17 = 8462696833
8462696833/839 = 10086647
10086647/1471 = 6857
6857/6857 = 1

So the check for x < n is the correct check to use in this case. If n wasn't being divided each time a factor was found, then the check for x*x < n would work.
 
  • Like
Likes   Reactions: CrazyNeutrino

Similar threads

  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 39 ·
2
Replies
39
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 80 ·
3
Replies
80
Views
10K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
5K
  • · Replies 17 ·
Replies
17
Views
7K
Replies
6
Views
5K
  • · Replies 175 ·
6
Replies
175
Views
27K