Vizible’s Blog

Python – Find Factors of Number

Posted in Python by wearetherock on August 30, 2009

Code :
========================================

from math import sqrt
import sys

input = int(sys.argv[1])
factors = []
min_fac = 2

while input > min_fac - 1 :
        if (input % min_fac) == 0 :
                factors.append(min_fac)
                input = input / min_fac
        else :
                min_fac += 1
print factors

~
Example :
========================================

$ python prime.py 600851475143
[71, 839, 1471, 6857]

~

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: