I'm very new to python, and I thought that I would create a program that returns the prime factors of a given number. This is my code:
import math
import operator
import functools
def isprime (n):
if n == 1:
return False
elif n == 2:
return True
else:
for x in range (2, int(math.sqrt(n))+1):
if n % x == 0:
return False
break
else:
return True
def factors (a):
factorlist = []
if isprime(a) == True:
print "The number is a prime."
else:
while functools.reduce(operator.mul, factorlist, 1) != a:
for x in range (1, a):
if a % x == 0:
if isprime(x) == True:
factorlist.append(x)
factorlist.sort()
print factorlist
testnumber = int(input("Enter a number."))
factors(testnumber)
My problem is that depending on the number, it takes a very long time. It can solve 100 or 1000 instantly, but 2000 or 864 just doesn't work! I left it running with 864 as my input for 45 minutes, but it didn't print anything. Is is something with the quality of my CPU? I'm running the program on a laptop.