Python highest prime factor problem.

In summary, the problem is finding the smallest number in a 2d array. The pseudo code above has a similar problem.
  • #1
rollcast
408
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
  • #2
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?
 
  • #3
Edit: Oops, didn't realize someone posted before me.

There's a while loop there that you forgot to reinitialize :wink:
 
  • #4
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
 
  • #5
You haven't fixed the problem Ninty mentioned.
 
  • #6
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?
 
  • #7
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(
\begin{array}{ccccc}
10 & 9 & 7 & 0 & 9 \\
6 & 10 & 6 & 2 & 7 \\
3 & 2 & 5 & 8 & 10 \\
\end{array}
\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.
 
  • #8
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(
\begin{array}{ccccc}
10 & 9 & 7 & 0 & 9 \\
6 & 10 & 6 & 2 & 7 \\
3 & 2 & 5 & 8 & 10 \\
\end{array}
\right) [/tex]

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

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?
 
  • #9
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(
\begin{array}{ccccc}
10 & 9 & 7 & 0 & 9 \\
6 & 10 & 6 & 2 & 7 \\
3 & 2 & 5 & 8 & 10 \\
\end{array}
\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.
 

Related to Python highest prime factor problem.

What is the "Python highest prime factor problem"?

The "Python highest prime factor problem" refers to a coding challenge where the goal is to find the largest prime factor of a given number using the Python programming language.

Why is finding the highest prime factor important?

Finding the highest prime factor of a number can be useful in many mathematical and computational applications, such as cryptography, data encryption, and data compression.

How do I approach solving the "Python highest prime factor problem"?

One approach is to first find all the factors of the given number, then check if each factor is prime, and finally return the largest prime factor. Another approach is to use the concept of prime factorization to directly find the highest prime factor.

What is the most efficient way to solve the "Python highest prime factor problem"?

The most efficient way to solve this problem is to use the concept of prime factorization, which involves dividing the given number by its smallest prime factor repeatedly until the quotient is 1. The last prime factor used in this process will be the highest prime factor of the given number.

Are there any built-in functions or libraries in Python that can help with this problem?

Yes, the math library in Python has a built-in function called isprime(n) that can check if a number n is prime or not. This function can be used to efficiently find the highest prime factor of a given number.

Similar threads

  • Programming and Computer Science
Replies
28
Views
1K
Replies
4
Views
2K
  • Programming and Computer Science
Replies
34
Views
3K
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
3
Replies
97
Views
7K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
29
Views
2K
Back
Top