Skip to content

Instantly share code, notes, and snippets.

@bcicen
Created May 5, 2014 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcicen/970bd8393976e3329c1c to your computer and use it in GitHub Desktop.
Save bcicen/970bd8393976e3329c1c to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
import sys
a = sys.argv[1]
print "finding prime factors of %d" % int(a)
def findfirstfactor(number):
b = 1
test = 1
factorlist = []
while test != 0:
b +=1
if b >= int(number):
return None
test = int(number) % int(b)
firstfactor = b
secondfactor = int(number) / b
factorlist.extend([firstfactor, secondfactor])
return factorlist
def testmorefactors(factors):
morefactors = []
if len(factors) == 1:
return None
for factor in factors:
test = findfirstfactor(factor)
if test is not None:
morefactors.append(factor)
if morefactors is None:
return None
else:
return morefactors
factors = findfirstfactor(a)
print factors
if factors is not None:
morefactors = testmorefactors(factors)
while morefactors:
for factor in morefactors:
factors.remove(factor)
for newfactors in findfirstfactor(factor):
factors.append(newfactors)
morefactors = testmorefactors(factors)
print (factors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment