Python highest prime factor problem.

  • Context: Python 
  • Thread starter Thread starter rollcast
  • Start date Start date
  • Tags Tags
    Prime Python
Click For Summary

Discussion Overview

The discussion revolves around a coding problem related to finding the largest prime factor of the number 600851475143, as posed in a homework context. Participants share their attempts at solving the problem using Python, focusing on the logic and structure of their code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant shares their initial code and expresses uncertainty about why it is not returning the correct highest prime factor.
  • Another participant suggests that the code may be printing the second highest factor instead of the highest prime factor.
  • There is a mention of a while loop that needs to be reinitialized, indicating a potential oversight in the code logic.
  • A participant provides an example of pseudo code to illustrate a similar problem, suggesting that fixing the logic in the example could help resolve the original issue.
  • Multiple participants discuss the structure of the loops and conditions in the code, pointing out potential logical errors and suggesting corrections.
  • There are repeated references to the need for proper initialization of variables within nested loops to avoid incorrect results.
  • One participant confirms that a suggested change to the code should work, but no definitive conclusion is reached regarding the correctness of the final code provided.

Areas of Agreement / Disagreement

Participants express differing views on the correctness of the code and the logic behind it. There is no consensus on a final solution, as multiple suggestions and corrections are offered without agreement on a single approach.

Contextual Notes

Some participants note issues related to variable initialization and loop structure, which may affect the correctness of the code. The discussion does not resolve these issues definitively.

Who May Find This Useful

Readers interested in programming, particularly in Python, and those looking to understand algorithms for finding prime factors may find this discussion relevant.

rollcast
Messages
403
Reaction score
0

Homework Statement


The problem is taken straight from the Project Euler website:

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

What is the largest prime factor of the number 600851475143 ?

The answer is 6857 as I must have solved it before but I can't figure out how I worked it out.


Homework Equations





The Attempt at a Solution



Code:
from math import *

number = 600851475143
number_sqrt = sqrt(number)
i = 1
j = 2
highest_prime_factor = 0

while i < number_sqrt:      #Loops while i is less than the square root of number
    if number % i == 0:     #Tests if i is a factor of number
        while j <= sqrt(i):     #Loops while j is less than square root of i
            is_prime = 1

            if i % j == 0:      #Tests if i is prime
                is_prime = 0
                break

            if j == trunc(sqrt(i)) and is_prime == 1:       #Checks if loop is finished and if i is prime
                highest_prime_factor = i        #Sets variable for highest prime factor as i

            j += 1
    i += 1

print highest_prime_factor

I'm not sure what wrong with my code but it seems to be printing the highest factor of the number.
 
Technology news on Phys.org
I think your code is probably printing the SECOND highest factor of the number, provided the highest factor wasn't a prime. Can you tell why that would happen?
 
Edit: Oops, didn't realize someone posted before me.

There's a while loop there that you forgot to reinitialize :wink:
 
I tried re writing the code but it didn't help as it still prints the non - prime factors?
Code:
import math

number = 600851475143
i = 2
j = 2
while i <= math.trunc(math.sqrt(number)):       #Loops while i is less than or equal to the square root of number
    if number % i == 0:     #Tests if i is a factor of number
        while j <= math.sqrt(i):        #Loops while j is less than or equal to the square root of i
            is_prime = 1
            if i % j == 0:  #Tests if j is a factor of i - breaks loops if true
                is_prime = 0
            if is_prime == 1 and j == math.trunc(math.sqrt(i)):        #Tests if j is not a factor i and if the loop if finished
                print i
            j += 1      #Increments j
    i += 1      #Increments i

The results it prints :
[Dbg]>>>
71
839
1471
6857
59569
104441
486847
 
You haven't fixed the problem Ninty mentioned.
 
kai_sikorski said:
You haven't fixed the problem Ninty mentioned.

I can't see why it shouldn't work unless I have messed up the indenting for the j while loop?
 
Okay here is an example pseudo code that has the a similar problem to your code. Say I get a 2d array and I want to find the smallest number in it.

[tex]A = \left(<br /> \begin{array}{ccccc}<br /> 10 & 9 & 7 & 0 & 9 \\<br /> 6 & 10 & 6 & 2 & 7 \\<br /> 3 & 2 & 5 & 8 & 10 \\<br /> \end{array}<br /> \right)[/tex]

Code:
N = 3 #number of rows
M = 5 #number of columns
smallest = A[SUB]11[/SUB]
i = 2
j = 1
[b]while[/b](j ≤ M)
    [b]while[/b](i ≤ N)
        [b]if[/b] A[SUB]ij[/SUB]<smallest
            smallest = A[SUB]ij[/SUB]
        [b]end[/b]
        i = i+1;
    [b]end[/b]
    j = j+1;
[b]end[/b]

This code would spit out 3. If you can fix this code you should be able to fix yours.
 
kai_sikorski said:
Okay here is an example pseudo code that has the a similar problem to your code. Say I get a 2d array and I want to find the smallest number in it.

[tex]A = \left(<br /> \begin{array}{ccccc}<br /> 10 & 9 & 7 & 0 & 9 \\<br /> 6 & 10 & 6 & 2 & 7 \\<br /> 3 & 2 & 5 & 8 & 10 \\<br /> \end{array}<br /> \right)[/tex]

Code:
N = 3 #number of rows
M = 5 #number of columns
smallest = A[SUB]11[/SUB]
i = 2
j = 1
[b]while[/b](j ≤ M)
    [b]while[/b](i ≤ N)
        [b]if[/b] A[SUB]ij[/SUB]<smallest
            smallest = A[SUB]ij[/SUB]
        [b]end[/b]
        i = i+1;
    [b]end[/b]
    j = j+1;
[b]end[/b]

This code would spit out 3. If you can fix this code you should be able to fix yours.


I'm not certain but is there an extra end in that code?

The red end breaks the red while loop and the blue one breaks the blue while loop, but the green one causes it to break early?
 
No I meant the end to go with the if.

The idea in the code is to fix the column, step over all the rows, then go to the next column and step over all the rows.. etc. But it does do that. Why not? Try pretending you're the computer and going through the algorithm step by step. Where does it go wrong.
 
  • #10
kai_sikorski said:
Okay here is an example pseudo code that has the a similar problem to your code. Say I get a 2d array and I want to find the smallest number in it.

[tex]A = \left(<br /> \begin{array}{ccccc}<br /> 10 & 9 & 7 & 0 & 9 \\<br /> 6 & 10 & 6 & 2 & 7 \\<br /> 3 & 2 & 5 & 8 & 10 \\<br /> \end{array}<br /> \right)[/tex]

Code:
N = 3 #number of rows
M = 5 #number of columns
smallest = A[SUB]11[/SUB]
j = 1
[b]while[/b](j ≤ M)
    i = 2
    [b]while[/b](i ≤ N)
        [b]if[/b] A[SUB]ij[/SUB]<smallest
            smallest = A[SUB]ij[/SUB]
        [b]end[/b]
        i = i+1;
    [b]end[/b]
    j = j+1;
[b]end[/b]

This code would spit out 3. If you can fix this code you should be able to fix yours.

Is that it now?
 
  • #11
Very close! What you wrote would return 2.
 
  • #12
kai_sikorski said:
Very close! What you wrote would return 2.

Should i equal 1?
 
  • #13
rollcast said:
Should i equal 1?

Yup!

Okay so now look at your code. You basically have a very similar mistake.
 
  • #14
Code:
import math

number = 600851475143
i = 2
while i <= math.trunc(math.sqrt(number)):       #Loops while i is less than or equal to the square root of number
    if number % i == 0:     #Tests if i is a factor of number
        j = 2
        while j <= math.sqrt(i):        #Loops while j is less than or equal to the square root of i
            is_prime = 1
            if i % j == 0:  #Tests if j is a factor of i - breaks loops if true
                break
            if is_prime == 1 and j == math.trunc(math.sqrt(i)):        #Tests if j is not a factor i and if the loop if finished
                print i
            j += 1      #Increments j
    i += 1      #Increments i
 
  • #15
Yup, I think that should work.
 

Similar threads

  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 36 ·
2
Replies
36
Views
2K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
10K
  • · Replies 34 ·
2
Replies
34
Views
6K
Replies
55
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K