HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Euler Project problem #3

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
problemprojecteuler

Problem

Problem: What is the largest prime factor of the number 600851475143?

Hi, I'm really new with Python. Can anyone give me some feedback for this code? it seem to produce the correct answer

a=600851475143
x=2
while x<(a/x):
    if a%x==0: a=a/x
    else: x+=1

print a

Solution

It produces the right answer in this particular case, but it wouldn't produce the right answer for all inputs. Consider a=4. The first time you hit the while guard, x is 2 and a/x is 2, so it skips the loop and prints 4.

Is there any reason for using an op= construct for x+=1 but not for a=a/x?

I'm not a Pythonista, but I'm pretty sure it's preferred style to put the if and else blocks on a new line even if they're only one line long:

if a%x==0:
        a/=x
    else:
        x+=1

Code Snippets

if a%x==0:
        a/=x
    else:
        x+=1

Context

StackExchange Code Review Q#155910, answer score: 10

Revisions (0)

No revisions yet.